android编程方式的布局权重

ran*_*tel 9 android android-layout android-linearlayout

我在布局xml上硬编码layout_weight.但是现在我想从java代码中给出layout_weight和weightSum.

我们如何从班上做到这一点?

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="25" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:background="#F51215"
        android:paddingLeft="5dip"
        android:text="5" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:background="#1AC92B"
        android:paddingLeft="5dip"
        android:text="20" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Vya*_*kin 36

像这样的东西:

               ......
               LinearLayout layout = (LinearLayout)findViewById(YOUR_LAYOT_ID);
               layout.setWeightSum(25f);
               LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) layout.getLayoutParams(); //or create new LayoutParams... 

               lParams.weight = 0.5f;
               .......
               someView.setLayoutParams(lParams);
               .......  
Run Code Online (Sandbox Code Playgroud)


Pad*_*mar 21

//set as like this below for different view set different float value.

myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));
Run Code Online (Sandbox Code Playgroud)

  • 请注意,与XML不同,您不应将高度或宽度设置为0,否则将不会显示布局. (13认同)

Jep*_*ppe 9

我只是想添加一个如何使用权重的示例,例如TableRow和TextViews:

           TableRow row = new TableRow(this);
           TableRow.LayoutParams params1 = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, 10f);
           row.setLayoutParams(params1);
           row.setPadding(10, 10, 10, 10);
           row.setBackgroundColor(R.color.black);

           TextView tv = new TextView(this);
           TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 6f);
           tv.setLayoutParams(params2);
           tv.setTextSize(30);
           tv.setBackgroundResource(R.color.white);

           TextView tv2 = new TextView(this);
           TableRow.LayoutParams params3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 2f);
           tv2.setLayoutParams(params3);
           tv2.setBackgroundResource(R.color.green);

           TextView tv3 = new TextView(this);
           TableRow.LayoutParams params4 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 2f);
           tv3.setLayoutParams(params4);
           tv3.setBackgroundResource(R.color.blue); 
Run Code Online (Sandbox Code Playgroud)

在下面生成TableRow.权重为10的父级,子级的宽度则等于宽度的60%,20%和20%.高度在这里由TextView,tv确定,其高度为30.希望它可以帮助某人.:-)

文本


Ajj*_*jji 5

这是你如何设置它。

 LinearLayout yourLayout=(LinearLayout)findViewById(R.id.container);
 yourLayout.setWeightSum(0.6f);
Run Code Online (Sandbox Code Playgroud)