Android布局重量

Android布局重量

我是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"
        >

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

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

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


慕村225694
浏览 368回答 3
3回答

ITMISS

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

www说

这样想,会更简单如果您有3个按钮,它们的权重相应为1,3,1,它将像HTML中的表格一样工作为该线提供5个部分:按钮1的1个部分,按钮2的3个部分和按钮1的1个部分
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android