1

我需要動態添加視圖到我的LinearLayout,我已經在我的xml文件中。我試圖添加布局並能夠添加它,但問題是我無法正確地將佈局權重屬性設置爲自定義添加的視圖。它始終存在問題。在Android中動態添加視圖與佈局權重屬性

這裏是我的XML(這我期待的觀點)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linear_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:layout_weight="1"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="100dp" 
     android:layout_weight="0.1" 
     android:text="text 1" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="100dp" 
     android:layout_weight="0.6" 
     android:text="text 2" /> 

    <CheckBox 
     android:layout_width="0dp" 
     android:layout_height="100dp" 
     android:layout_weight="0.3" 
     android:text="Check Box 1" /> 

</LinearLayout> 

他是我在哪裏動態地添加視圖來佈局我的Java代碼

public class MyActivity extends Activity { 

    private ViewGroup mLinearLayout; // this should be your main layout where your planning to add the views programatically 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout1); 
     mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout); 
     addLayout("text 1", "text 2", "check box"); 
    } 

    private void addLayout(String textView1Text, String textView2Text, String checkBoxText) { 
     LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f); 
     mLinearLayout.setLayoutParams(param); 

     TextView tv1 = new TextView(this); 
     tv1.setText(textView1Text); 
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT , 0.1f); 
     tv1.setLayoutParams(lp); 
     mLinearLayout.addView(tv1); 


     TextView tv2 = new TextView(this); 
     tv2.setText(textView2Text); 
     LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT , 0.6f); 
     tv2.setLayoutParams(lp1); 
     mLinearLayout.addView(tv2); 

     CheckBox cb = new CheckBox(this); 
     cb.setText(textView2Text); 
     LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT , 0.3f); 
     lp2.weight = 0.1f; 
     cb.setLayoutParams(lp2); 
     mLinearLayout.addView(cb); 

    } 
} 

請幫我圖解決問題。 TIA

+0

你得到了什麼錯誤?你能發佈輸出的任何屏幕截圖嗎? –

+0

沒有錯誤,但重量屬性不適用/不能正常工作 – NatuGadu

+0

嘗試在LayoutParam構造函數中設置寬度0。 – chandil03

回答

2

問題是你的佈局PARAMS。你需要像下面一樣使用

private void addLayout(String textView1Text, String textView2Text, String checkBoxText) { 
     LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f); 
     mLinearLayout.setLayoutParams(param); 

     TextView tv1 = new TextView(this); 
     tv1.setText(textView1Text); 
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100); 
     lp.weight = 0.1f; 
     tv1.setLayoutParams(lp); 
     mLinearLayout.addView(tv1); 


     TextView tv2 = new TextView(this); 
     tv2.setText(textView2Text); 
     LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0, 100); 
     lp.weight = 0.6f; 
     tv2.setLayoutParams(lp1); 
     mLinearLayout.addView(tv2); 

     CheckBox cb = new CheckBox(this); 
     cb.setText(textView2Text); 
     LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(0, 100); 
     lp.weight = 0.3f; 
     cb.setLayoutParams(lp2); 
     mLinearLayout.addView(cb); 

    } 
+0

這對我有效。感謝名單 – NatuGadu

1

試試這個,

變化

  1. 新增mLinearLayout.setOrientation(LinearLayout.HORIZONTAL)
  2. 寬度= 0
  3. 你添加了一些其他的線性佈局my_linear_layout

    私人無效addLayout(字符串textView1Text,字符串textView2Text,字符串checkBoxText){

    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100, 1.0f); 
        mLinearLayout.setLayoutParams(param); 
        mLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 
    
        TextView tv1 = new TextView(this); 
        tv1.setText(textView1Text); 
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT , 0.1f); 
        tv1.setLayoutParams(lp); 
        mLinearLayout.addView(tv1); 
    
    
        TextView tv2 = new TextView(this); 
        tv2.setText(textView2Text); 
        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT , 0.6f); 
        tv2.setLayoutParams(lp1); 
        mLinearLayout.addView(tv2); 
    
        CheckBox cb = new CheckBox(this); 
        cb.setText(textView2Text); 
        LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT , 0.3f); 
        cb.setLayoutParams(lp2); 
        mLinearLayout.addView(cb); 
    

    }

+0

是的。但也需要添加自定義高度。我的意思是100 DP。 +1 –

+0

你可以將它添加到** LinearLayout.LayoutParams ** –

相關問題