我是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#####] |
我尝试了太多不同的组合,我甚至都记不住了,但都没有奏效。
发布于 2011-02-14 05:44:35
它不起作用,因为您使用fill_parent作为宽度。当总和大于LinearLayout时,权重用于分配剩余的空闲空间或取走空间。将你的宽度设置为0dip,它会起作用。
发布于 2011-09-13 13:51:06
当你没有像fill_parent
这样给宽度附加固定值时,可以使用android:Layout_weight
。
这样做:
<Button>
Android:layout_width="0dp"
Android:layout_weight=1
-----other parameters
</Button>
发布于 2011-11-04 23:48:39
这样想,会更简单
如果你有3个按钮,它们的权重是1,3,1,它的工作方式就像HTML中的表格
为该行提供5个部分:1部分用于按钮1,3部分用于按钮2,1部分用于按钮1
https://stackoverflow.com/questions/4986861
复制相似问题