如何简化布局?

时间:2012-02-27 18:50:50

标签: android android-layout

我是一名新手开发者,我创建了一个符合我期望的布局,但我担心它的结构过于复杂。

我使用了很多布局但是我没有看到如何在不混合线性和相对布局的情况下构建类似的布局,是否可能?

如何简化此布局? 谢谢你的想法。

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

<LinearLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/black_blue_gradient"
    android:paddingBottom="5dip"
    android:paddingTop="24dip" >
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/header" >

    <LinearLayout
        android:id="@+id/linearlayout01left"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="40"
        android:orientation="horizontal" >

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

            <LinearLayout
                android:id="@+id/footer"
                android:layout_width="fill_parent"
                android:layout_height="8dp"
                android:layout_alignParentBottom="true"
                android:background="@drawable/b_bgradient" >
            </LinearLayout>

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/footer"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/oranwall_ovosh" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearlayout02right"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="60"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#E9EEF2"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/My_txt"
            android:textColor="#535A5F"
            android:textSize="14sp" />
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

试试吧。不要忘记把你的背景和其他资源放在

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

    <TableRow
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:background="@drawable/ic_launcher">
    </TableRow>

    <TableRow
        android:id="@+id/body"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="3">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:src="@drawable/perm" />

                <TableRow
                    android:id="@+id/footer"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:background="@drawable/ic_launcher" >
                </TableRow>

            </LinearLayout>

        </LinearLayout>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </TableRow>

</LinearLayout>

答案 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="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <!-- I don't understand the point of this one: -->
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/black_blue_gradient"
        android:paddingBottom="5dip"
        android:paddingTop="24dip" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/linearlayout01left"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:orientation="horizontal" >

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

                <!-- I don't understand the point of this one -->
                <LinearLayout
                    android:id="@+id/footer"
                    android:layout_width="match_parent"
                    android:layout_height="8dp"
                    android:layout_alignParentBottom="true"
                    android:background="@drawable/b_bgradient" >
                </LinearLayout>

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/footer"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:src="@drawable/oranwall_ovosh" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearlayout02right"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="0.6"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="#E9EEF2"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/My_txt"
                android:textColor="#535A5F"
                android:textSize="14sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>