我可以在另一个linearLayout中添加在XML中定义的linearLayouts,其中父级包含许多布局。例如,在这种情况下,我有一个父元组LinearLayout必须元素:scrollView和LinearLayout。 scrollView有一个LinearLayout,最初在XML中定义为空。我想为此LinearLayout添加布局,以便在用户超出屏幕底部时,布局可以在scrollView中。
public class ActivityMain extends Activity implements OnFocusChangeListener{
EditText date;
LinearLayout row[]=new LinearLayout[30];
int rowNo=0;
LinearLayout itemForm=(LinearLayout)LinearLayout.inflate(this, R.id.item_form,null);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date=(EditText)findViewById(R.id.date);
date.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1==false){
row[rowNo++]=(LinearLayout)findViewById(R.layout.row);
itemForm.addView(row[rowNo]);
}
}
}
这是包含父LinearLayout及其scrollView和LinearLayout
的XML布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ActivityMain"
android:orientation="vertical"
android:id="@+id/linearLayout"
android:weightSum="10">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="8"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
<EditText
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
<TextView android:id="@+id/code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="voucher Code"
android:textSize="30sp"/>
</LinearLayout>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/item_form"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
我希望在另一个linearLayout
中充气的LinearLayout<?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="horizontal"
android:id="@+id/row" >
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter item name"
android:id="@+id/item"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Quantity"
android:id="@+id/quant" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Rate"
android:id="@+id/quant" />
</LinearLayout>
答案 0 :(得分:2)
尝试使用以下方法来膨胀xml:
View viewToLoad = LayoutInflater.from(
getApplicationContext()).inflate(
R.layout.yourlayoutfilename, null);
((LinearLayout) findViewById(R.id.item_form)).addView(viewToLoad);
对于Addview:
LinearLayout itemForm=(LinearLayout) viewToLoad.findViewById(R.id.item_form);
itemForm.addView(.......);