以编程方式设置Android布局权重

14

我在布局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>
6个回答

44

类似这样:

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

我无法为某个视图作为 TextView 进行此操作。还有其他的解决方案吗? - Mikaël Mayer
1
@NarayanaJ,你确定吗?http://developer.android.com/reference/android/widget/LinearLayout.html#setWeightSum(float) - Vyacheslav Shylkin
@VyacheslavShilkin,感谢您的指出。我似乎失去了尝试这个方法的上下文,但是当我将该方法放入我的“Activity”中时,Studio会抛出错误,因此我认为该方法已被弃用。显然,该方法仍在API中。对于匆忙的评论,我很抱歉,我应该删除它吗?干杯! - Narayana J

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

myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));

15
请注意,与XML不同的是,您不应将布局的高度或宽度设置为0,否则该布局将不会显示。 - TheIT
@TheIT 可能现在已经改变了。将宽度设置为零可以解决问题。 - Jarin Rocks

13

我只是想举一个例子来说明如何在包含TextView的TableRow中使用weightsum:

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

生成表格行。父元素的weightsum为10,子元素的宽度分别为宽度的60%、20%和20%。高度由TextView "tv" 决定,其高度为30。希望对某人有所帮助。 :-)

text


7

如果您想通过编程方式更改父级权重并保留其子属性,则可以使用以下方法

以下是设置方法:

 LinearLayout yourLayout=(LinearLayout)findViewById(R.id.container);
 yourLayout.setWeightSum(0.6f);

2
使用LinearLayout动态生成具有权重的TextView
LinearLayout lin_hoizontal = new LinearLayout(context);
lin_hoizontal.setOrientation(LinearLayout.HORIZONTAL);
lin_hoizontal.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 10f));
lin_hoizontal.setPadding((int) context.getResources().getDimension(R.dimen.d_8), (int) context.getResources().getDimension(R.dimen.d_2), (int) context.getResources().getDimension(R.dimen.d_8), (int) context.getResources().getDimension(R.dimen.d_2));

android.widget.LinearLayout.LayoutParams params_label = new android.widget.LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.5f);
TextView txt_label = new TextView(context);
txt_label.setTextColor(context.getResources().getColor(R.color.listing_header_txt_color));//your text color
txt_label.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.d_13));
txt_label.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
txt_label.setLayoutParams(params_label);
txt_label.setPadding(0, 0, (int) context.getResources().getDimension(R.dimen.d_2), 0);
txt_label.setText("Label");


android.widget.LinearLayout.LayoutParams params_value = new android.widget.LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 7.5f);
TextView txt_value = new TextView(context);
txt_value.setTextColor(context.getResources().getColor(R.color.listing_header_txt_color));
txt_value.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.d_13));
txt_value.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
txt_value.setLayoutParams(params_value);
txt_value.setPadding((int) context.getResources().getDimension(R.dimen.d_2), 0, 0, 0);
txt_value.setText("Value");

lin_hoizontal.addView(txt_label);
lin_hoizontal.addView(txt_value);
lin_hoizontal.addView(lin_main);

0

我喜欢Ajii的回答或者:

((LinearLayout)view).setWeightSum(n);

对于个人视图权重:

((LinearLayout.LayoutParams)view.getLayoutParams()).weight = n;
  • 当然可以这样做... LinearLayout view = findViewById(R.id.view_name);
    (并且可以省略动态转换 (LinearLayout))

  • 或者替换为... findViewById(R.id.view_name) 在上面的视图中。

这两种方法对我都有效。 如果你想在dimens文件中使用数值:

<item name="new_weight" format="float" type="dimen">(your number)</item>

并且:float n = getResources().getFloat(R.dimen.new_weight);

我相信你已经了解大部分,但是... 我曾经也是个新手,我总是喜欢额外的描述。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接