Android的布局:父宽度的一半 [英] Android Layout: width half of parent
问题描述
我有一个非常简单的布局,我不能让它看起来像我想要的。这是一个按钮和一个开关一个LinearLayout中。我希望他们能够显示一个在另一个之上,但我想它们的宽度是父布局的一半。
I have a very simple layout that I can't make it looks like I want. It's a LinearLayout with a button and a Switch. I want them to show one above the other, but I want their width to be the half of the parent layout.
|--LinearLayout---------|
| |
| ----------- |
|| Switch | |
| ----------- |
| ----------- |
|| button | |
| ----------- |
------------------------
我一直在寻找其他类似的回答在左右,但我无法找到一个解决方案,为我工作。这是我试过至今:
I've been looking at other similar answer in SO but I couldn't find a solution that works for me. This is what I've tried so far:
<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
的孩子,但它使它们消失
With this, the button and the switch take all the space of the parent instead of take only the half.
I've tried with android:layout_width = 0dp
in the children but it makes they disappear.
任何帮助,请?
推荐答案
一种可能的方式是有一个主水平的LinearLayout一个分割的宽度为2,它里面有垂直布局
One possible way is to have a master horizontal LinearLayout that splits the width to 2, and inside it have the vertical layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
<!-- Right side spacer -->
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
</LinearLayout>
这篇关于Android的布局:父宽度的一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!