在XML布局中忽略按钮填充属性

时间:2013-07-04 14:33:13

标签: android

我正在尝试在屏幕上以2x3(w / h)的形式设置6个按钮的布局。

我的布局XML如下:

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

除了android:padding="4dip"标记内的<Button>之外,此XML主要按照我的意愿运行。这个填充似乎被完全忽略了,使得按钮在中间被挤压在一起。

下面我提供了左边应用程序当前状态的粗略图(看看我的绘画技巧) - 以及右边应用程序的预期布局(蓝色框是按钮)。

Current vs Intended layout

我有什么想法可以将按钮分开在中心?

2 个答案:

答案 0 :(得分:8)

您必须添加margin而不是padding,这取决于您必须应用保证金的位置(layout_marginLeftlayout_marginRight),但这可以解决您的问题:

android:layout_margin="4dp"

而不是

android:padding="4dp"

答案 1 :(得分:1)

你在这里使用的布局太多了!

您需要父RelativeLayout。在其中,添加第一个按钮Button1。然后第二个按钮将被放置toRightOf: Button1,其中包含您需要的填充(android:marginLeft = xdp),在顶部对齐。

同样,第三个按钮将为toBottomOf: Button1填充,并在左侧对齐。诸如此类...