[英]Buttons in RelativeLayout fill screen width
As you can see above, I have four rows of Buttons
in a RelativeLayout
. 如您在上面看到的,我在
RelativeLayout
有四行Buttons
。 How can I have the Buttons
cover the screen width evenly? 如何使
Buttons
均匀覆盖屏幕宽度?
I've tried all sorts of things but nothing has worked: I tried setting the RelativeLayout
width to match_parent
and making all the Button's
layout_width
= fill_parent
and all sorts of things like that. 我尝试了各种方法,但没有任何效果:我尝试将
RelativeLayout
宽度设置为match_parent
,并使所有Button's
layout_width
= fill_parent
以及fill_parent
所有东西。
Any ideas? 有任何想法吗?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Game" >
<RelativeLayout
android:id = "@+id/rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<Button
android:id="@+id/button0"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button1"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_toRightOf="@+id/button0"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button2"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_toRightOf="@+id/button1"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button3"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_toRightOf="@+id/button2"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
...
...
...
<Button
android:id="@+id/button12"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_below="@+id/button8"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button13"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_below="@+id/button9"
android:layout_toRightOf="@+id/button12"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button14"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_below="@+id/button10"
android:layout_toRightOf="@+id/button13"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button15"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_below="@+id/button11"
android:layout_toRightOf="@+id/button14"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
</RelativeLayout>
</RelativeLayout>
The best way is to have four vertical Linear layouts which each contain their buttons in a horizontal Linear Layout. 最好的方法是具有四个垂直线性布局,每个布局均包含其按钮在水平线性布局中。 Make sure that all the buttons width is
fill_parent
确保所有按钮的宽度均为
fill_parent
Then set the weight attribute to each of the made the LinearLayouts width = match_parent and set the Buttons layout_weight = "1"made the LinearLayouts width = match_parent and set the Buttons layout_weight = "1" to 1 like this: 然后将权重属性设置为使LinearLayouts width = match_parent的每个属性,并设置Buttons layout_weight =“ 1”使LinearLayouts width = match_parent的按钮设置为Button 1,如下所示:
android:layout_weight = "1"
Make sure the Linear layouts width are also set to match_parent
确保线性布局的宽度也设置为
match_parent
Basically this adds up the weights and divides the screen width into between the buttons. 基本上,这会增加权重并将屏幕宽度划分为按钮之间。
The solution to your problem is in weights. 您的问题的解决方案是权重。
Hope this helps. 希望这可以帮助。 Happy coding :)
快乐的编码:)
PS If this answers your question please mark this as the correct answer PS:如果此回答您的问题,请将其标记为正确答案
You could get the screen dimensions and use them to decide the buttons size 您可以获取屏幕尺寸并使用它们来确定按钮的尺寸
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
and then use these sizes to determine the size of your buttons (each size would be the width divided by 4) 然后使用这些尺寸确定按钮的尺寸(每个尺寸都是宽度除以4)
Fiddling around with this should get you where you want 随便摆弄一下应该可以让您到达想要的地方
Why don't you use LinearLayout
and weight
property for a row? 为什么不连续使用
LinearLayout
和weight
属性? It could be something like this: 可能是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Game" >
<RelativeLayout
android:id = "@+id/rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<LinearLayout
android:id="@+id/ll_row_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button0"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:layout_weight="1"
android:tag="0"/>
<Button
android:id="@+id/button1"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_weight="1"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button2"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:layout_weight="1"
android:tag="0"/>
<Button
android:id="@+id/button3"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:layout_weight="1"
android:tag="0"/>
</LinearLayout>
...
...
...
<LinearLayout
android:id="@+id/ll_row_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/ll_row_3">
<Button
android:id="@+id/button12"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:background="@drawable/button"
android:layout_weight="1"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button13"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_weight="1"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button14"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_weight="1"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
<Button
android:id="@+id/button15"
android:layout_width="@dimen/size"
android:layout_height="@dimen/size"
android:layout_weight="1"
android:background="@drawable/button"
android:onClick="ButtonOnClick"
android:tag="0"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
If it won't work, try to set buttons' android:layout_width
property to 0dp
如果不起作用,请尝试将按钮的
android:layout_width
属性设置为0dp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.