如何将复选框动态添加到 Android 布局? [英] How to add CheckBoxes dynamically to an Android layout?
问题描述
我必须动态添加CheckBox到我的Activity布局中,布局的XML如下`
I have to add CheckBox dynamically to my Activity layout, the XML of the layout is as follows `
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentSV"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/parentLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<RelativeLayout
android:id="@+id/feedbackRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/feedbackCustomerNameLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Name : "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/feedbackCustomerName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
android:id="@+id/feedbackPlansLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/feedbackCustomerNameLL"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Plans Explained"
android:textColor="@android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/feedbackPlansCheckBoxLL"
android:orientation="horizontal" >
<!--
Required to Add CheckBoxes Here Dynamically
-->
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
`
复选框将被添加到评论区.如何动态添加它们,因为我必须根据服务器发送的数据在运行时添加它们.我无法从层次结构中删除 ScrollView 或任何其他视图.
CheckBoxes are to be added in the commented area. How to add them dynamically, because I have to add them in runtime based on data sent from server. I cannot remove the ScrollView or any other view from the hierarchy.
推荐答案
给该布局一个 id
例如...
Give that layout an id
such as...
<LinearLayout
android:id="@+id/check_add_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/feedbackPlansCheckBoxLL"
android:orientation="horizontal" >
<!--
Required to Add CheckBoxes Here Dynamically
-->
</LinearLayout>
使用给定的 id 初始化父布局...
Initialize a parent layout using the given id as...
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.check_add_layout);
然后创建您的复选框...
Then create your CheckBox...
CheckBox checkBox = new CheckBox(this);
checkBox.setId(id);
checkBox.setText("text");
创建有关其大小、填充、对齐方式的参数...
Create parameters about its size, padding, alignment...
LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
checkParams.setMargins(10, 10, 10, 10);
checkParams.gravity = Gravity.CENTER;
现在将这个新创建的 CheckBox
添加到该父布局...
now add this newly created CheckBox
to the that parent layout...
parentLayout.addView(checkBox, checkParams);
这篇关于如何将复选框动态添加到 Android 布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!