在一个layout格局中插入其他的layout布局或view组件——方法

在一个layout布局中插入其他的layout布局或view组件——方法
本帖最后由 u014587769 于 2015-05-05 21:08:56 编辑
我想实现做用4个xml 的 layout 布局实现在两个activity之间的无限切换
我的目的是探究方法的种类(能理解到原因更好):

以下是已经完成的
我的问题是这个小例子中的  xml文件中的include  节点的代码能不能用java 代码在.java文件中实现


{
    <include layout="@layout/image_view" />

    <include layout="@layout/button" />
}

  


layout文件中的  activity_main.xml   文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainXml"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是“主”layout" />

    <include layout="@layout/image_view" />

    <include layout="@layout/button" />

</LinearLayout>

layout文件中的  activity_main_another.xml   文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二Layout" />

    <include layout="@layout/image_view" />

    <include layout="@layout/button" />

</LinearLayout>

layout文件中的  button.xml   文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@+id/myselfButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试按钮" />

</LinearLayout>

layout文件中的  inage_view.xml   文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_image_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/myselfImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

将牛button 和 ImageView 插入到main后预览
在一个layout格局中插入其他的layout布局或view组件——方法
在一个layout格局中插入其他的layout布局或view组件——方法
----------------------------------------------------------------------------------------------------------------------------------------------
                  -----------------------------------分---割---线---------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------
java代码
anotherActivity.java

package com.example.onelayoutinsertanotherlayout___test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class anotherActivity extends Activity
{
private Button myButton;

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_another);

Button myButton = (Button) findViewById(R.id.myselfButton);
myButton.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(anotherActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
});

}
}

MainActivity.java

package com.example.onelayoutinsertanotherlayout___test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity
{
private Button myButton;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button myButton = (Button) findViewById(R.id.myselfButton);
myButton.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
anotherActivity.class);
startActivity(intent);
finish();
}
});
}
}

AndroidManifest.xml文件

 <activity
            android:name=".MainActivity"
            android:label="第一个Activity对应第一个layout" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.onelayoutinsertanotherlayout___test.anotherActivity"
            android:label="另一个Activity对应另一个layout" >
        </activity>




------解决思路----------------------
LayoutInflater mInflater=LayoutInflater.from(context);
View=mInflater.inflate(R.layout.image_view,null);
------解决思路----------------------
用viewpager,
------解决思路----------------------
类似1楼,xml布局当作view来使用