【问题标题】:How to change layout_weight in my View (programmatically)如何在我的视图中更改 layout_weight(以编程方式)
【发布时间】:2014-08-21 00:04:05
【问题描述】:

如何使用 Java 代码从 XML 更改我的 View 对象的 layout_weight?我在下面包含了我尝试的最后一件事。 vynosy 是我想要设置的属性值。

View hospVyslLineAppColor = (View) view.findViewById 
                            (R.id.hospodarsky_vysledok_line_appcolor);

hospVyslLineAppcolor.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT,
            Math.abs((float)vynosy)));

我的 XML:

<View
     android:id="@+id/hospodarsky_vysledok_line_appcolor"
     android:layout_width="match_parent"
     android:layout_height="20dp"
     android:layout_weight="69382"
     android:background="#a8a8a8" />

【问题讨论】:

  • 您的视图的父级是 LinearLayout 吗?
  • 是的,我在一个线性中有 2 个相同的视图 ... 使用类似“horizo​​ntal graf”
  • 嘿 Tormino,您需要以编程方式设置权重的目的是什么?显示图形之类的东西?

标签: android android-layout view android-layout-weight


【解决方案1】:

您可以将其用于线性布局

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT;  
params.weight = 1;

【讨论】:

    【解决方案2】:

    我认为这会对你有所帮助...Set Width Programatically on SO

    但我认为不可能以编程方式更改此属性,但只能在新属性中设置新的布局(一般查看)。

    【讨论】:

      【解决方案3】:

      您需要了解 Layout_weights 的工作原理。仅当您有多个视图时,这些才有效。您的代码是正确的,只是您需要再添加一个视图。两者都以线性布局声明。您需要在其中应用权重的布局。

       View hospVyslLineAppColor = (View) view.findViewById (R.id.hospodarsky_vysledok_line_appcolor);
      
              hospVyslLineAppcolor.setLayoutParams(new LinearLayout.LayoutParams(
              LayoutParams.MATCH_PARENT,
              LayoutParams.WRAP_CONTENT,
              Math.abs((float)vynosy))); // see to it the value of vynosy is less than 1
      
       View hospVyslLineAppColor1 = (View) view.findViewById (R.id.hospodarsky_vysledok_line_appcolor);
      
              hospVyslLineAppColor1.setLayoutParams(new LinearLayout.LayoutParams(
              LayoutParams.MATCH_PARENT,
              LayoutParams.WRAP_CONTENT,
              (1 - Math.abs((float)vynosy)))); // Will complement the weight
      

      然后重写你的 XML 文件:

          <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical" >
      
                <!-- layout_height is 0dp as height is to be set by weight factor -->
                 <View
                    android:id="@+id/hospodarsky_vysledok_line_appcolor"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" 
                    android:layout_weight="0.4"
                    android:background="#a8a8a8" />
      
                <!-- layout_height is 0dp as height is to be set by weight factor -->
                 <View
                    android:id="@+id/hospodarsky_vysledok_line_appcolor1"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.6"
                    android:background="#a8a8a8" />
              </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多