[英]Get two buttons 50/50 width in my layout
i've got a layout that looks like below. 我有一个看起来像下面的布局。 But ideally I want the buttons Save and Cancel to be 50% width each, next to each other.
但是理想情况下,我希望“保存”和“取消”按钮的宽度分别为50%,彼此相邻。 Can't quite seem to get them to work though.
似乎不能完全让他们工作。
Any suggestions? 有什么建议么?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="-" />
<EditText
android:id="@+id/txtCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnPlus"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/btnPlus"
android:layout_toRightOf="@+id/btnMinus"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="+" />
<Button
android:id="@+id/btnGo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnMinus"
android:layout_toRightOf="@+id/button1"
android:maxWidth="100dp"
android:text="Save"
android:width="100dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnMinus"
android:text="Cancel" />
</RelativeLayout>
You shouldn't be using a RelativeLayout
for this design. 您不应为此设计使用
RelativeLayout
。 You should be using a LinearLayout
. 您应该使用
LinearLayout
。
Something like this outline (very stripped-down): 像这样的轮廓(非常精简):
<LinearLayout android:orientation=vertical>
<LinearLayout android:orientation=horizontal>
<Button android:layout_width=100 /> <!-- Minus -->
<EditText android:layout_width=0 android:layout_weight=1 />
<Button android:layout_width=100 /> <!-- Plus -->
</LinearLayout>
<LinearLayout android:orientation=horizontal>
<Button android:layout_width=0 android:layout_weight=1 /> <!-- Cancel -->
<Button android:layout_width=0 android:layout_weight=1 /> <!-- Save -->
</LinearLayout>
</LinearLayout>
Your looking for android:layout_weight="0.5"
. 您正在寻找
android:layout_weight="0.5"
。 This only works with LinearLayouts, though. 不过,这仅适用于LinearLayouts。
So what you could do is add a LinearLayout that contains your two buttons and set the weights then. 因此,您可以做的是添加一个包含两个按钮的LinearLayout,然后设置权重。
您应该仅在按钮的RelativeLayout
内使用LinearLayout
,并为其赋予相同的android:layout_weight
(根据需要选择1和1、2和2)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.