2014-02-18 35 views
1

我要動態地添加複選框,我的活動佈局,該佈局的XML如下: `如何將CheckBox動態添加到Android佈局?

<?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或任何其他視圖。

回答

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

舉一個ID到你的LinearLayout並做

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

簡單。 :)是不是。希望這有助於:)

+0

我不喜歡這樣,但我得到空指針異常的'linear.addView(CB);' – Rahul

+0

你叫的LinearLayout線性=(LinearLayout中)findViewById('your id');在添加複選框之前?你也可以在你添加的地方發佈代碼嗎? –

+1

並且從佈局中可以看到,沒有標識已分配給要添加複選框的線性佈局。所以可能的機會是你可能正在訪問別的東西。也檢查一下。 –

1

按照以下步驟動態地添加複選框:

第1步:分配ID來LinearLayout在XML中的代碼訪問:

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

第2步:代碼中的訪問佈局:

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

步驟3:添加複選框佈局:

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); 

} 
+0

我試過了,但它也給'NullPointer Exception' – Rahul

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

給該佈局的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 = 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);