在 Android 中使用布局权重属性动态添加视图

Nat*_*adu 4 android android-layout android-linearlayout android-layout-weight

我需要将视图动态添加到我LinearLayout的 xml 文件中已有的视图中。我尝试添加布局并能够添加它,但问题是我无法正确将布局权重属性设置为自定义添加的视图。它总是有问题。

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

他是我的 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);

    }
}
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题。TIA

Kin*_*ses 5

问题出在你的布局参数上。你需要像下面这样使用

   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);   
       lp1.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);  
       lp2.weight =  0.3f;
       cb.setLayoutParams(lp2);
       mLinearLayout.addView(cb);

    }
Run Code Online (Sandbox Code Playgroud)