簡體   English   中英

在我的布局中獲得兩個50/50寬度的按鈕

[英]Get two buttons 50/50 width in my layout

我有一個看起來像下面的布局。 但是理想情況下,我希望“保存”和“取消”按鈕的寬度分別為50%,彼此相鄰。 似乎不能完全讓他們工作。

有什么建議么?

在此處輸入圖片說明

<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>

您不應為此設計使用RelativeLayout 您應該使用LinearLayout

像這樣的輪廓(非常精簡):

<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>

您正在尋找android:layout_weight="0.5" 不過,這僅適用於LinearLayouts。

因此,您可以做的是添加一個包含兩個按鈕的LinearLayout,然后設置權重。

您應該僅在按鈕的RelativeLayout內使用LinearLayout ,並為其賦予相同的android:layout_weight (根據需要選擇1和1、2和2)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM