避免布局冗余

时间:2013-07-11 16:49:44

标签: android android-layout dry

我想在行的右侧有按钮B1和B2,按钮A1和A2在该行的剩余空间中居中。

Centering within remainder

如果没有包含按钮A 两次

,这是否可行?

以下是详细说明具体问题。

Eclipse通过说“此LinearLayout布局或RelativeLayout父布局可能无用”来批评两个父布局@id/buttonAx

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/myRightMostLL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >

        <Button
            android:id="@+id/buttonB1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B1" />

        <Button
            android:id="@+id/buttonB2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B2" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/myRightMostLL">

        <LinearLayout
            android:id="@+id/remainderLL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true">

            <Button
                android:id="@+id/buttonA1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="A1" />

            <Button
                android:id="@+id/buttonA2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="A2" />

        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

然而,我需要这种无用的能够在Buttons B布局之后将剩余空间中的按钮A居中。可以使用Buttons A的一个父包装器布局获得相同的效果吗?

在像这样的布局中,我也经常质疑@ id / myRightMostLL是否真的需要在文件中出现 first

2 个答案:

答案 0 :(得分:1)

下面的解决方案没有显示可能无用的警告:)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_toLeftOf="@+id/layRight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        >
                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/button4"
                    android:text="Button" />

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/button2"
                    android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

    </RelativeLayout>


</RelativeLayout>

http://clip2net.com/s/5nna7M

答案 1 :(得分:1)

我认为你可以用一个布局来做这件事,使用一些“填充”视图来占用空间:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <View
        android:layout_width="0"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A1" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A2" />
    <View
        android:layout_width="0"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A3" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A4" />
</LinearLayout>

如果您的目标是API 14或更高版本,则可以在上面使用Space小部件而不是ViewSpace只是View的轻量级替代品,专为此类设计而设计。

相关问题