两个布局文件:
一个main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
    <LinearLayout 
        android:id="@+id/add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"></LinearLayout></LinearLayout>
一个test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <ListView 
            android:id="@+id/caiList"
            android:layout_width="400dp"
            android:layout_height="wrap_content"></ListView>
        
        <LinearLayout 
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            
            <ImageView 
                android:id="@+id/caiImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"/>
            
            <TextView 
                android:text="简洁"
                android:id="@+id/jianjie"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            
        </LinearLayout>
        
  </LinearLayout>  一个java文件:
public class TestAddViewActivity extends Activity {
private LinearLayout page;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        page=(LinearLayout) this.findViewById(R.id.add);
        
        View view1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.test, null);
        
        page.addView(view1);
    }
}
想法就是在一个布局文件中引用另一个布局文件,设置布局,但是貌似addView没起作用,什么都没显示

解决方案 »

  1.   

    出现的问题就是text.xml里面的内容并没有显示出来
      

  2.   

    test.xml中  <LinearLayout 
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">明显两个不能同时为fp,第二个改成wrap_content就ok了。
      

  3.   

    又是一个被xml欺骗的楼主,用viewgroup可以轻松搞定任何布局。
      

  4.   

    page=(LinearLayout) this.findViewById(R.id.add);
    page.addView(view1);view1加在page上,检查下看page有木有问题