1

I'm trying to setup a layout of 6 buttons in the form of 2x3 (w/h) on the screen.

My layout XML is below:

<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"
android:background="@drawable/androidbg"
android:paddingBottom="@dimen/none"
android:paddingLeft="@dimen/none"
android:paddingRight="@dimen/none"
android:paddingTop="@dimen/none"
tools:context=".MainActivity" >

<LinearLayout
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="4dip"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnTopLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/diary"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                style="?android:attr/buttonBarButtonStyle"
                android:onClick="openDiary"
                android:padding="4dip" />

            <Button
                android:id="@+id/btnTopRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/diary"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                style="?android:attr/buttonBarButtonStyle"
                android:onClick="openDiary"
                android:padding="4dip" />
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="4dip"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnMidLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/diary"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                style="?android:attr/buttonBarButtonStyle"
                android:onClick="openDiary"
                android:padding="4dip" />

            <Button
                android:id="@+id/btnMidRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/diary"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                style="?android:attr/buttonBarButtonStyle"
                android:onClick="openDiary"
                android:padding="4dip" />
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="4dip"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnBottomLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/diary"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                style="?android:attr/buttonBarButtonStyle"
                android:onClick="openDiary"
                android:padding="4dip" />

            <Button
                android:id="@+id/btnBottomRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/diary"
                android:layout_weight="1"
                android:background="@drawable/menu_button"
                style="?android:attr/buttonBarButtonStyle"
                android:onClick="openDiary"
                android:padding="4dip" />
        </LinearLayout>
    </RelativeLayout>    

</LinearLayout>

This XML mostly works as I want it to - with the exception of the android:padding="4dip" inside the <Button> tags. This padding seems to be being completely ignored, making the buttons squashed together in the middle.

Below I have provided a crude drawing (behold my paint skills) of the current state of the app on the left - and the intended layout of the app on the right (blue boxes are buttons).

Current vs Intended layout

Any ideas how I would be able to pad the buttons apart in the center?

4

2 回答 2

8

您必须添加margin而不是padding,这取决于您必须在哪里应用边距(layout_marginLeftlayout_marginRight),但这应该可以解决您的问题:

android:layout_margin="4dp"

代替

android:padding="4dp"
于 2013-07-04T14:35:21.667 回答
1

You are using too many layouts here!

You need a parent RelativeLayout. Inside it, add the first button, Button1. Then the second button will be placed toRightOf: Button1 with the padding you need (android:marginLeft = xdp), aligned at the top.

Similarly, the third button will be toBottomOf: Button1 with the padding, and aligned at the left side. Etcetera...

于 2013-07-04T14:38:28.033 回答