Android的布局重量布局、重量、Android

2023-09-12 00:12:38 作者:27.一季烟雨凉

我是新的Andr​​oid开发,我有一个关于线性布局设定的重量问题。

I am new to Android development and I have a question about setting weight in a linear layout.

我想有两个自定义按键和一个自定义编辑文本创建一个行。编辑文本应该只需要尽可能多室作为其内容,和两个按钮应该水平扩展填补行中的可用空间的其余部分。像这样...

I am trying to create a row with two custom buttons and a custom edit text. The edit text should only take up as much room as its content, and the two buttons should expand horizontally to fill the rest of the available space in the row. Like this...

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

经过多次尝试,这是最接近我能得到我想要的,即使它似乎过于复杂了。

After several attempts this is the closest I can get to what I want, even though it seems overly complicated.

| [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>

据我所知设置[机器人:layout_weight =2]举行的按钮应该让他们承担比编辑文本更多空间的线性布局,但它确实相反对我来说,让他们俩的一半大小。

As I understand setting [android:layout_weight="2"] for the linear layouts holding the buttons should make them take up more room than the edit text, but it does the opposite for me, making them both half the size.

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

我已经尝试了很多不同的组合,我甚至不记得他们,并没有工作过。

I have tried so many different combinations I can't even remember them, and none have worked.

推荐答案

由于您使用FILL_PARENT的宽度它不工作。重量被用于分配剩余空的空间的或的带走空间时的总和比所述的LinearLayout大。设置你的宽度为 0dip 来代替,而它的工作。

It doesn't work because you are using fill_parent as the width. The weight is used to distribute the remaining empty space or take away space when the total sum is larger than the LinearLayout. Set your widths to 0dip instead and it will work.