我是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,它将起作用.
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)
| 归档时间: |
|
| 查看次数: |
109677 次 |
| 最近记录: |