开发者

android how to achieve this layout

I have 4 buttons in 2x2 matrix. let them be b11 b12 b21 b22.

I want the alignment as

______________________________
|                            |
|         b11 b12            |
|         b21 b22            |
|____________________________|

where b11 right edge and b12's left egde match layout's center b21's right edge matches b11's right开发者_高级运维 edge and b22's left edge matches b12's left edge


Mind you this isn't 100% correct code, just the idea, I'm to lazy to type out android:layout_width and height 7 times

<LinearLayout vertical>
 <LinearLayout Horizontal>
   <view layout_width="0px" weight="1"/>
   <view layout_width="0px" weight="1"/>
 </linearlayout>     
 <LinearLayout Horizontal>
   <view layout_width="0px" weight="1"/>
   <view layout_width="0px" weight="1"/>
 </linearlayout>
</LinearLayout>


This is hand-wavy and without code, but I would create a LinearLayout that contains each row of 2 buttons. Set the LinearLayout's gravity to center, and width to wrap_content, then give the buttons equal widths. Repeat for the second row.


Try to use this:

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout 
        android:id="@+id/linearLayout1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="center"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">

     <LinearLayout 
        android:id="@+id/linearLayout1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
         <Button android:text="B11" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="B12" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        </LinearLayout> 

        <LinearLayout 
        android:id="@+id/linearLayout1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button android:text="B21" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="B22" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

        </LinearLayout>

    </LinearLayout>

I think this is what you want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜