【问题标题】:Android Layout WeightAndroid 布局权重
【发布时间】:2011-06-26 14:09:59
【问题描述】:

我是 Android 开发新手,我有一个关于在线性布局中设置权重的问题。

我正在尝试创建一个包含两个自定义按钮和一个自定义编辑文本的行。编辑文本应该只占用与其内容一样多的空间,并且两个按钮应该水平扩展以填充行中剩余的可用空间。像这样……

| [#Button++#] [#Button--#] [et] |

经过几次尝试,这是我能得到的最接近我想要的东西,尽管它看起来过于复杂。

| [Button] [Button] [###ET###] |

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center_vertical"
    android:layout_weight="1"
    >

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <RelativeLayout 
            android:id="@+id/btn_plus_container" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            >
            <ImageButton 
                android:layout_height="wrap_content"
                android:layout_width="fill_parent" 
                android:id="@+id/btn_plus" 
                android:background="@drawable/btn"
                android:layout_centerInParent="true"
                ></ImageButton>
            <TextView 
                android:textColor="#FAFAF4"
                android:id="@+id/tv_dice_plus" 
                android:text="@string/tv_plus" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                ></TextView>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <RelativeLayout 
            android:id="@+id/btn_minus_container" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            >
            <ImageButton 
                android:layout_height="wrap_content"
                android:layout_width="fill_parent" 
                android:id="@+id/btn_minus" 
                android:background="@drawable/btn"
                android:layout_centerInParent="true"
                ></ImageButton>
            <TextView 
                android:textColor="#FAFAF4"
                android:id="@+id/btn_minus" 
                android:text="@string/tv_minus" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                ></TextView>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <EditText 
            android:textColor="#FAFAF4"
            android:text="@string/et_output" 
            android:id="@+id/et_output" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:inputType="number"
            android:selectAllOnFocus="true" 
            android:minWidth="50sp"         
            android:maxWidth="50sp"
            android:background="@drawable/tv_black_background3"
            android:textColorHighlight="#c30505"
            ></EditText>
    </LinearLayout>
</LinearLayout>

据我了解,为按住按钮的线性布局设置 [android:layout_weight="2"] 应该会使它们占用比编辑文本更多的空间,但它对我来说正好相反,使它们都缩小了一半。

| [Btn] [Btn] [#####et#####] |

我尝试了很多不同的组合,我什至不记得它们,但都没有奏效。

【问题讨论】:

    标签: android layout


    【解决方案1】:

    它不起作用,因为您使用的是 fill_parent 作为宽度。当总和大于LinearLayout时,权重用于分配剩余的空白空间带走空间。将宽度设置为 0dip 即可。

    【讨论】:

    • 将线性布局的宽度设置为 0dip 就像我想要的那样工作。我不知道这是一个选择。这是有道理的,为什么这会在 wrap_content 不起作用的地方起作用。当没有内容时将宽度设置为 wrap_content 会使其消失,但将其设置为 0dip 则可以使其从无延伸到需要的大小。非常感谢。
    • 将 layout_width 设置为“fill_parent”,将 layout_weight 设置为浮点值,如“0.2”、“0.85”等也可以。在这种情况下,数字越大,视图的尺寸就越小。你能解释一下这是否是一种可以接受的方式吗?
    • 这对我也有用。谢谢你。 (虽然我不得不说,有没有人觉得 android 布局系统很奇怪和不直观?)
    • 谢谢!我真的很难说出为什么我的布局宽度没有按预期显示。是 0dip 设置做到了 :)
    • @tstyle 如果你想做简单的事情,Android 很容易。然而,如果你想做不仅仅是简单的事情,那就太难了。
    【解决方案2】:

    android:Layout_weight 可以在您没有像 fill_parent 等那样将固定值附加到您的宽度时使用。

    做这样的事情:

    <Button>
    
    Android:layout_width="0dp"
    Android:layout_weight=1
    
    -----other parameters
    </Button>
    

    【讨论】:

      【解决方案3】:

      这样想,会更简单

      如果你有 3 个按钮,并且它们的权重相应地是 1,3,1,它将像 HTML 中的表格一样工作

      为该行提供 5 部分:按钮 1 的 1 部分、按钮 2 的 3 部分和按钮 1 的 1 部分

      【讨论】:

        【解决方案4】:

        在linearLayout中设置WeightSum=2;

        并按照您希望他们显示的方式将权重分配给其子项。我已将权重 ="1" 分配给子项。因此两者都将分配总数的一半。

         <LinearLayout
            android:id="@+id/linear1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2"
            android:orientation="horizontal" >
        
            <ImageView
                android:id="@+id/ring_oss"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/ring_oss" />
        
            <ImageView
                android:id="@+id/maila_oss"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/maila_oss" />
        </LinearLayout>
        

        【讨论】:

          【解决方案5】:

          权重值 0-1 共享可用空间的分布(设置 layout_width="0px" 后)与权重值成比例。未指定权重(没有权重条目)的视图元素的权重为 0,这意味着它们没有得到扩展。

          一个不需要权重条目的简单替代方法是将选取框附加到带有文本的视图 它告诉它从文本所需的最小值(wrap_content)扩展到可用空间 编辑文本: android:layout_width="wrap_content" android:ellipsize="marquee"

          【讨论】:

            【解决方案6】:

            我想将EditTexts 的宽度设置为wrap_content,并将两个按钮放入宽度为fill_parent 且权重设置为1 的LinearLayout

            【讨论】:

              【解决方案7】:

              我发现的另一个原因(听起来可能很模糊)。以下方法无效。

              线性布局垂直

              LinearLayout 高度 fillparent + weight

              LinearLayout 高度 fillparent + weight

              LinearLayout 高度 fillparent + weight

              EndLinearLayout

              做了什么

              相对布局

              线性布局垂直

              LinearLayout 高度 fillparent + weight

              LinearLayout 高度 fillparent + weight

              LinearLayout 高度 fillparent + weight

              EndLinearLayout

              EndRelativeLayout

              带有线性和权重的根布局听起来很模糊,它不起作用。当我说“没用”时,我的意思是,在我查看了各种分辨率之间的图形布局之后,屏幕一致性大打折扣。

              【讨论】:

                【解决方案8】:
                <LinearLayout
                    android:id="@+id/linear1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:weightSum="9"
                    android:orientation="horizontal" >
                
                    <ImageView
                        android:id="@+id/ring_oss"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="3"
                        android:src="@drawable/ring_oss" />
                
                    <ImageView
                        android:id="@+id/maila_oss"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="3"
                        android:src="@drawable/maila_oss" />
                <EditText
                        android:id="@+id/edittxt"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="3"
                        android:src="@drawable/maila_oss" />
                </LinearLayout>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-06-19
                  • 2011-02-11
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多