簡體   English   中英

android編程方式的布局權重

[英]android Layout weight programmatically

我在布局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>

像這樣的東西:

               ......
               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);
               .......  
//set as like this below for different view set different float value.

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

我只是想添加一個如何使用權重的示例,例如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); 

在下面生成TableRow。 權重為10的父級,子級的寬度則等於寬度的60%,20%和20%。 高度在這里由TextView,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;
  • 當然你可以做.. LinearLayout view = findViewById(R.id.view_name);
    (並刪除動態轉換(LinearLayout))

  • 或者替換...... ......... .. findViewById(R.id.view_name)用於上面的視圖。

這些都對我有用。 如果你想在dimension文件中有價值:

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

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

我相信你已經知道了大部分但是......我曾經是新手,而且我總是很欣賞額外的描述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM