Android布局重量

Unl*_*vil 84 layout android

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

我正在尝试使用两个自定义按钮和自定义编辑文本创建一行.编辑文本应占用与其内容相同的空间,并且两个按钮应水平扩展以填充行中的其余可用空间.像这样...

| [#Button++#] [#Button--#] [et] |
Run Code Online (Sandbox Code Playgroud)

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

| [Button] [Button] [###ET###] |
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

据我所知设置[android:layout_weight ="2"]为线性布局按住按钮应该使它们占用比编辑文本更多的空间,但它对我来说却相反,使它们都是一半大小.

| [Btn] [Btn] [#####et#####] |
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多不同的组合,我甚至都记不住它们,但没有一个有效.

Rom*_*Guy 213

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

  • 这对我也有用.谢谢.(虽然我不得不说,有没有其他人发现android布局系统怪异且不直观?) (37认同)
  • 将线性布局上的宽度设置为0dip就像我想要的那样工作.我不知道那是一个选择.有意义的是,为什么这会在wrap_content不会的地方起作用.当没有内容时将宽度设置为wrap_content会使其消失,但将其设置为0dip可以使其从无到有延伸到需要的大小.非常感谢你. (6认同)

lea*_*drd 14

android:Layout_weight当您没有将固定值附加到您的宽度时,可以使用等fill_parent.

做这样的事情:

<Button>

Android:layout_width="0dp"
Android:layout_weight=1

-----other parameters
</Button>
Run Code Online (Sandbox Code Playgroud)


Geo*_*yen 7

这样想,会更简单

如果您有3个按钮,它们的权重相应为1,3,1,它将像HTML中的表格一样工作

为该线提供5个部分:按钮1的1个部分,按钮2的3个部分和按钮1的1个部分