我有这样的数据
<LinearLayout> ===> vertical
<LinearLayout> ===>Horizontal
<TextView> <TextView> <TextView> <TextView>
<LinearLayout> ===>Horizontal
<TextView> <TextView> <TextView> <TextView>
<LinearLayout> ===>Horizontal
<TextView> <TextView> <TextView> <TextView>
<LinearLayout>
如何动态添加这些布局?
谢谢
答案 0 :(得分:0)
我会帮助你完成我所做的代码。请注意,我不会为您编码(现在仍然帮助某人来自SO)。如果您有任何疑问,请随意。
在这里,我在spinner
的{{1}}上添加了button
和linear layout
,这些都是动态的:
onClick
这就是我检索 else if(v == btnAddChild)
{
LinearLayout dynamicLayout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER_VERTICAL;
dynamicLayout.setLayoutParams(params);
dynamicLayout.setOrientation(LinearLayout.HORIZONTAL);
int layoutId = totalDynamicChild + 1000; //dynamicLayout always have dynamic spinnser's id + 1000 to avoid same id
dynamicLayout.setId(layoutId);
Spinner spinner = new Spinner(this);
spinner.setLayoutParams(params);
spinner.setAdapter(spinChildAdapter);
spinner.setId(totalDynamicChild);
spinnderIdList.add(totalDynamicChild);
ImageButton btn = new ImageButton(this);
btn.setImageDrawable(getResources().getDrawable(R.drawable.action_delete));
btn.setBackgroundResource(0);
btn.setLayoutParams(params);
int btnId = totalDynamicChild + 2000; //btn always have dynamic spinnser's id + 2000 to avoid same id
btn.setId(btnId);
dynamicLayout.addView(spinner);
dynamicLayout.addView(btn);
parentSpinner.addView(dynamicLayout);
totalDynamicChild++;
值的方法:
spinner's
答案 1 :(得分:0)
使用ScrollView作为父级为垂直LinearLayout创建一个xml,因此每当LinearLayout no of child更多时,它将应用scroll。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
另一个垂直LinearLayout子级的xml,它在Horizontal LinearLayout中包含4个TextView。
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
现在如何将项目添加到垂直LinearLayout:
private LinearLayout container;
private Context context;
private int size=3;
private int counter=1;
context = this;
container = (LinearLayout) findViewById(R.id.container);
for (int i=0;i<size;i++){
View view = LayoutInflater.from(context).inflate(R.layout.row_item,null);
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
TextView textView2 = (TextView) view.findViewById(R.id.textView2);
TextView textView3 = (TextView) view.findViewById(R.id.textView3);
TextView textView4 = (TextView) view.findViewById(R.id.textView4);
textView1.setText(String.valueOf(counter++));
textView2.setText(String.valueOf(counter++));
textView3.setText(String.valueOf(counter++));
textView4.setText(String.valueOf(counter++));
view.setTag((i+1));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,String.valueOf((Integer)v.getTag()),Toast.LENGTH_SHORT).show();
}
});
container.addView(view);
}