4
****Main.xml**** 

这是我的 main.xml 布局,我想在滚动视图的线性布局内膨胀另一个布局。所以请有人建议我如何在滚动视图中膨胀另一个布局。

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:id="@+id/productDetailsLayout"

        >
           <LinearLayout
                android:id="@+id/productDetailsLayout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 

                >
                <TextView   android:layout_width="fill_parent"
                android:layout_height="wrap_content"  />

                </LinearLayout>
           <LinearLayout
                android:id="@+id/productDetailsLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 

                >          
      <ScrollView android:id="@+id/scview"
          android:layout_width="fill_parent"
                android:layout_height="fill_parent"

                >
         <LinearLayout
                android:id="@+id/productDetailsLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 

                >


                </LinearLayout>

    </ScrollView>
    </LinearLayout>
    </LinearLayout>

请查看另一个布局 dynamic_lyaout.xml。

当我给 xml 充气时,就会发生异常。所以请有人帮忙

动态布局.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/abc"
               >

                <ImageView
                    android:id="@+id/icon"
                    android:layout_width="80dp"
                    android:layout_height="80dp"

                    android:paddingLeft="10dp"
                    android:paddingRight="10dp" />

                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/icon"
                    android:paddingBottom="10dp"
                    android:paddingLeft="5dp"
                    android:text="Title"
                    android:textColor="#CC0033"
                    android:textSize="16dp" />
                 <TextView
                    android:id="@+id/price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/icon"
                    android:layout_below="@+id/title"

                    android:paddingBottom="10dp"
                    android:paddingLeft="5dp"

                    android:text="Price:"
                    android:textColor="#Cd0023"
                    android:textSize="16dp" />

                <TextView
                    android:id="@+id/desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/price"
                    android:layout_toRightOf="@+id/icon"
                    android:paddingLeft="5dp"
                    android:text="Description"
                    android:textColor="#3399FF"
                    android:textSize="14dp" />
                     <View
                        android:layout_width="fill_parent"
                        android:layout_height="1dp"
                        android:background="#880808" 
                        android:layout_below="@+id/desc"/>        

            </RelativeLayout>

请在下面输入我的代码

 LinearLayout mainlayou = (LinearLayout)findViewById(R.id.productDetailsLayout);
     ScrollView scview=(ScrollView)mainlayou.findViewById(R.id.scview);
     LinearLayout scviewLayout1 = (LinearLayout)scview.findViewById(R.id.productDetailsLayout1);
    for(int i=0; i<5; i++){
     LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     scviewLayout1.addView(inflater.inflate(R.layout.dynamic_layout,
             mainlayou, false));
}
4

3 回答 3

5

试试下面的代码:

inflator_layout.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" >

<LinearLayout
                android:id="@+id/llDIsplayForToDoInflater"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@color/aliceblue"
                android:gravity="center" >

                <TextView
                    android:id="@+id/txtdt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"  
                    android:textSize="20sp" >
                </TextView>
</LinearLayout>
</LinearLayout>

Java 文件:

LinearLayout llDisplayData, llChangeBG;
TextView tvtxt;

llDisplayData = (LinearLayout) findViewById(R.id.productDetailsLayout1);
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView;

llDisplayData.removeAllViews();

customView = linflater.inflate(R.layout.inflator_layout, null);

llChangeBG = (LinearLayout) customView.findViewById(R.id.llDIsplayForToDoInflater);

tvtxt = (TextView) customView.findViewById(R.id.txtdt);
llDisplayData.addView(customView);
于 2013-10-16T07:02:50.017 回答
0

试试这个,在你的滚动视图中添加这个代码,你会得到你的包含布局。

<ScrollView android:id="@+id/scview"
      android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
     <LinearLayout
            android:id="@+id/productDetailsLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
           >
   <include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/your_layour" />
   </LinearLayout>

</ScrollView>
于 2013-10-16T06:55:34.433 回答
0
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                    v = inflater.inflate(R.layout.dynamic_layout, productDetailsLayout1, false);
                    //content.addView(v);
                    LinearLayout scrolloyouts=(LinearLayout)findViewById(R.id.productDetailsLayout1);

                    LinearLayout loginlayout=(LinearLayout)v.findViewById(R.id.abc);
                    scrolloyouts.removeAllViews();
                    scrolloyouts.addView(abc);
于 2014-03-27T06:49:29.283 回答