如何动态地将CheckBoxes添加到Android布局?

时间:2014-02-18 12:16:32

标签: android xml

我必须动态地将CheckBox添加到我的Activity布局中,布局的XML如下所示 `

<?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>

`

CheckBoxes将添加到注释区域中。如何动态添加它们,因为我必须根据服务器发送的数据在运行时添加它们。我无法从层次结构中删除ScrollView或任何其他视图。

5 个答案:

答案 0 :(得分:5)

为该布局指定id,例如......

<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初始化父布局为...

LinearLayout parentLayout = (LinearLayout) findViewById(R.id.check_add_layout);

然后创建CheckBox ...

CheckBox checkBox = new CheckBox(this);
checkBox.setId(id);
checkBox.setText("text");

创建有关其大小,填充,对齐的参数......

LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
checkParams.setMargins(10, 10, 10, 10);
checkParams.gravity = Gravity.CENTER;

现在将新创建的CheckBox添加到该父布局...

parentLayout.addView(checkBox, checkParams);

答案 1 :(得分:1)

按照以下步骤动态添加CheckBox:

第1步:在xml中为LinearLayout分配ID,以便在代码中进行访问:

<LinearLayout
      android:id="@+id/yourlayout"
 ....  
/>

第2步:访问代码中的布局:

LinearLayout linear=(LinearLayout)findViewById(R.id.yourlayout);

第3步:将CheckBox添加到布局:

LayoutParams lparams = new LayoutParams(
   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

for(int i=0;i<your_count;i++){

    CheckBox checkBox = new CheckBox(context); 
    checkBox.setLayoutParams(lparams); 

    linear.addView(checkBox);

}

答案 2 :(得分:1)

试试这个:

LinearLayout layout = (LinearLayout) findViewById(R.id.yourlayout);
CheckBox chkTeamName = new CheckBox(this);
chkTeamName.setText(teamName.get(i));
chkTeamName.setTextColor(Color.BLACK);
layout.addView(chkTeamName);

答案 3 :(得分:0)

CheckBox checkBox = new CheckBox(context);
// set the properties including layoutparams
// Then add it to parent
Parent.addView(checkBox);

答案 4 :(得分:0)

为您的LinearLayout提供ID并执行

LinearLayout linear = (LinearLayout) findViewById('your id');
Checkbox cb = new CheckBox(this);
//Give customization to it
linear.addView(cb);

简单。 :)不是吗希望这会有所帮助:)