【问题标题】:android Layout weight programmaticallyandroid布局权重以编程方式
【发布时间】:2012-07-23 11:22:36
【问题描述】:

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

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    类似这样的:

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

    【讨论】:

    • 我不能这样做,因为 someView 是一个 TextView。还有其他解决方案吗?
    • @VyacheslavShilkin,感谢您的指出。我似乎已经失去了为什么要尝试这个的上下文,但是当我将该方法放入我的 Activity 时,Studio 抛出了一个错误,由于某种原因,让我相信该方法已被弃用。显然,该方法在 API 中非常重要。不好意思评论太仓促了,要不要删?干杯!
    【解决方案2】:
    //set as like this below for different view set different float value.
    
    myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));
    

    【讨论】:

    • 请注意,与 XML 不同,您不应将高度或宽度设置为 0,否则布局将不会出现。
    • @TheIT 现在可能已经改变了。设置宽度为零有效。
    【解决方案3】:

    我只是想添加一个示例,说明如何将 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。 weightsum 为 10 的父级,子级的宽度则等于宽度的 60%、20% 和 20%。这里的高度是由高度为 30 的 TextView、tv 确定的。希望对某人有所帮助。 :-)

    【讨论】:

      【解决方案4】:

      如果您想以编程方式更改父权重并且它是子属性,请在下面使用

      这是你的设置方式。

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          我喜欢阿吉的回答或者:

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

          对于个人视图权重:

          ((LinearLayout.LayoutParams)view.getLayoutParams()).weight = n;
          
          • 当然可以……LinearLayout view = findViewById(R.id.view_name);
            (并删除动态转换(LinearLayout))

          • 或替换....……..findViewById(R.id.view_name) 以获取上面的视图。

          这些都对我有用。 如果你想要尺寸文件中的值:

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

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

          我相信您已经知道其中的大部分内容,但是...我曾经是新手,我一直很欣赏额外的描述。

          【讨论】:

            猜你喜欢
            • 2012-07-06
            • 2011-03-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-12
            • 1970-01-01
            • 2017-01-10
            相关资源
            最近更新 更多