我是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#####] |
我尝试了很多组合,甚至记不清楚具体有哪些,但都没有起作用。