Android 布局:宽度为父级的一半
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20000560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Layout: width half of parent
提问by mario595
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。我希望它们显示一个在另一个之上,但我希望它们的宽度是父布局的一半。
|--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:
我一直在查看 SO 中的其他类似答案,但找不到适合我的解决方案。这是我迄今为止尝试过的:
<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>
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.
有了这个,按钮和开关占据了父级的所有空间,而不是只占据一半。我android:layout_width = 0dp
在孩子们身上试过,但它让他们消失了。
Any help, please?
请问有什么帮助吗?
回答by yoah
One possible way is to have a master horizontal LinearLayout that splits the width to 2, and inside it have the vertical layout
一种可能的方法是拥有一个主水平 LinearLayout 将宽度拆分为 2,并在其内部具有垂直布局
<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>
回答by Tommaso Resti
The trick is to use 0dpfor the size you want manage via layout_weight! Try this
诀窍是将0dp用于您要通过layout_weight管理的大小!尝试这个
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:weightSum="2" >
<Switch
android:id="@+id/remember_me_switch"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="@string/remember" />
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="loginOnclick"
android:text="@string/login_button_text" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
</LinearLayout>
</LinearLayout>
回答by Apoorv
Try this code
试试这个代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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" />
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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" />
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
回答by Kalai.G
Button button = (Button) findViewById(R.id.share_button);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int buttonWidth = width/2;
set this buttonWidth
to your button like button.setWidth(buttonWidth);
将此设置buttonWidth
为您的按钮button.setWidth(buttonWidth);
回答by giuseppe
int params = linearLayout.getWidth()/2;
ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));