android布局权重编程

android布局权重编程,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我已经在布局xml上硬编码了布局权重,但是现在我想从java代码中给出布局权重和权重总和 我们如何在课堂上做到这一点 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="25" > <TextView

我已经在布局xml上硬编码了布局权重,但是现在我想从java代码中给出布局权重和权重总和

我们如何在课堂上做到这一点

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

类似这样的内容:

               ......
               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的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%。此处的高度由文本视图tv确定,tv的高度为30。希望它能帮助别人。:-)

以下是您的设置方式

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

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

我喜欢Ajii的回答或者:

((LinearLayout)view).setWeightSum(n);
对于单个视图权重:

((LinearLayout.LayoutParams)view.getLayoutParams()).weight = n;
  • 你当然可以<代码>线性布局视图=findViewById(R.id.view\U名称)
    (并放弃动态浇铸(线性布局))

  • 或替代<代码>findViewById(R.id.view\u name)用于上述视图

这两个都对我有用。 如果要在dimens文件中输入值:

<item name="new_weight" format="float" type="dimen">(your number)</item>
(您的号码)
和:
float n=getResources().getFloat(R.dimen.new_-weight)


我相信你已经知道了大部分,但是。。。我曾经是一个新手,我一直很欣赏这些额外的描述。

我不能这样做,因为someView是一个文本视图。还有别的解决办法吗?@NarayanaJ,你确定吗@VyacheslavShilkin,谢谢你指出。我似乎已经失去了尝试此方法的背景,但由于某种原因,当我将该方法放入我的
活动
时,Studio抛出了一个错误,使我相信该方法已被弃用。显然,这种方法在API中非常常见。对不起,我的评论太仓促了,我要删除它吗?干杯请注意,与XML不同,您不应将高度或宽度设置为0,否则布局将不会显示。@It现在可能已更改。将宽度设置为零起作用。
<item name="new_weight" format="float" type="dimen">(your number)</item>