使用相对布局将屏幕分为四个相等的部分

时间:2015-02-22 17:21:06

标签: java android layout relative linear

我目前有代码允许我将屏幕分成四个相等的部分,每个部分都有一个图像按钮。但是,代码使用线性布局,这提出了“嵌套权重对性能不利”的建议。如何使用相对布局进行布局? 下面是代码,这是预期格式的图片
enter image description here          

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"

        android:orientation="horizontal"
        android:background="#ff191919">

        <ToggleButton
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff414141"
            android:layout_weight="1.0"
            android:text="Plane"
            android:layout_margin="5dp"
            android:textSize="20sp"
            android:onClick="airplaneClicked"/>


        <ImageButton
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff414141"
            android:layout_weight="1.0"
            android:layout_margin="5dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_brightness"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:orientation="horizontal"
        android:background="#ff191919">01

        <ImageButton
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff414141"
            android:layout_weight="1.0"
            android:layout_margin="5dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_sound" />


        <ImageButton
            android:id="@+id/settingsbutton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff414141"
            android:layout_weight="1.0"
            android:layout_margin="5dp"
    android:scaleType="fitCenter"
    android:src="@drawable/settings2"/>

    </LinearLayout>
    </LinearLayout>

3 个答案:

答案 0 :(得分:1)

使用以下xml:

<?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:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#242425"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#d5d5d5" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#d5d5d5"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#d5d5d5"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#242425"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#d5d5d5" />

    </LinearLayout>


</LinearLayout>

答案 1 :(得分:0)

为RelativeLayout尝试此代码。

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

    <RelativeLayout
        android:id="@+id/r3"
        android:layout_width="match_parent"
        android:layout_height="225dp"
        android:layout_marginBottom="10dp"
        android:layout_alignParentTop="true" >

        <ImageButton
            android:id="@+id/i1"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/i2"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/r2"
        android:layout_width="match_parent"
        android:layout_height="225dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/r3" >

        <ImageButton
            android:id="@+id/i3"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/i4"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />
    </RelativeLayout>

</RelativeLayout>

答案 2 :(得分:0)

试试这个

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_light"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_light"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_light"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>
相关问题