2012-07-23 30 views
6

我在布局xml.hard上编写了layout_weight,但现在我想给出来自java代码的layout_weight和weightSum。android以编程方式安排布局体重

我们该怎么做呢?

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

回答

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

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

注意,与XML,你不应该高度或宽度设置为0,动态生成的TextView与重量的布局根本不会出现,否则。 – TheIT 2014-01-09 04:02:46

33

事情是这样的:

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

我不能为someView作为一个TextView做到这一点。其他解决方案? – 2013-08-21 12:57:29

+0

不幸的是,没有'setWeightSum()'这样的方法了...... – 2016-04-30 11:11:48

+1

@NarayanaJ,你确定吗? http://developer.android.com/reference/android/widget/LinearLayout.html#setWeightSum(Float) – 2016-05-06 09:33:02

5

我只是想补充的如何使用weightsum与例如用TextViews通过tablerow内的例子:

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

主要生产下面的TableRow。其权重为10的父母,然后孩子的宽度等于宽度的60%,20%和20%。这里的高度由TextView,tv决定,它的高度为30.希望它可以帮助某人。 :-)

text

2

这里是你如何设置它。

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

使用的LinearLayout

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