****Main.xml****
这是我的main.xml布局,我希望在scrollview的线性布局中扩展另一个布局。所以请有人建议我如何在滚动视图中充气另一个布局。
<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充气时会发生异常。所以请有人帮忙
dynamic_layout.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));
}
答案 0 :(得分:4)
尝试以下代码:
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);
答案 1 :(得分:1)
答案 2 :(得分: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>
答案 3 :(得分: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);