开发者

How can I layout two buttons to be the same size?

开发者 https://www.devze.com 2023-04-04 18:53 出处:网络
The layout definition is below. Basically I want the two buttons to be the same width, but right now their width is determined by the string they display, is there a better way to lay this out such th

The layout definition is below. Basically I want the two buttons to be the same width, but right now their width is determined by the string they display, is there a better way to lay this out such that the buttons are the same width and that the Button+EditText combo fills the width of the screen?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/editorlinearlayout" 
    android:orientation="vertical"
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:gravity="center">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button android:id="@+id/setNameBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Set Item Name "/>
        <EditText android:id="@+id/nameTxtBox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button android:id="@+id/setGroupBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set Group Name"/>
        <EditText android:id="@+id/groupTxtBox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

Edit: I've tried using a TableLayout as was suggested below, but now the EditText's don't fill the remaining portion of the screen. See the image and layout below.

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <TableRow>
        <Button android:id="@+id/setNameBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            a开发者_如何学运维ndroid:text=" Set Item Name "/>
        <EditText android:id="@+id/nameTxtBox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </TableRow>
    <TableRow>
        <Button android:id="@+id/setGroupBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set Group Name"/>
        <EditText android:id="@+id/groupTxtBox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </TableRow>
    </TableLayout>

How can I layout two buttons to be the same size?


the trick is to combine the tableview defined in the edit above with a strechColumn tag:

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">
  <TableRow>
    <Button android:id="@+id/setNameBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Set Item Name "/>
    <EditText android:id="@+id/nameTxtBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
  </TableRow>
</TableLayout>


Use a TableLayout and TableRows.


Three options:

  1. Put this in a TableLayout instead of the nested LinearLayout thing you are currently doing
  2. Change your nesting from a vertical parent with horizontal children to a horizontal parent with vertical children (however, this opens up possible vertical positioning issues, so I don't favor this option)
  3. Use a RelativeLayout and all of the layout_* attributes that RelativeLayout children have available to them

I'd favor option 1, personally.


you can use specific width and height for laout_width/layout_height, as in layout_width="150dip". You run the risk of having a button that can't fit all your text though, but if your text is fixed, you can double check to make sure it's all good.

0

精彩评论

暂无评论...
验证码 换一张
取 消