【发布时间】:2013-11-15 11:56:39
【问题描述】:
我有一个非常简单的布局,我无法让它看起来像我想要的那样。这是一个带有按钮和开关的线性布局。我希望它们显示在另一个之上,但我希望它们的宽度是父布局的一半。
|--LinearLayout---------|
| |
| ----------- |
|| Switch | |
| ----------- |
| ----------- |
|| button | |
| ----------- |
------------------------
我一直在寻找其他类似的答案,但我找不到适合我的解决方案。这是我迄今为止尝试过的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
这样,按钮和开关占用了父级的所有空间,而不是只占用一半。
我试过在孩子们身上使用android:layout_width = 0dp,但它会让他们消失。
有什么帮助吗?
【问题讨论】:
-
layout_weight仅适用于orientation指定的维度。也就是说,权重适用于垂直的高度和水平线性布局的宽度。