如何在相对布局中放置两个布局

时间:2014-03-24 21:37:41

标签: android layout relative

我正在尝试将两个布局放在彼此之上。我使用相对布局并将它们放在相对布局中。但问题是我只显示了其中一个布局,而后者是后者。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="bottom|center"
    android:orientation="vertical"
    android:weightSum="1" 
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:background="#00ACE7"
        android:orientation="vertical"
        android:weightSum="1"  >

        <ImageView
            android:id="@+id/telenor_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/telenor_logo"
            android:src="@drawable/telenor_logo" />

        <ImageView
            android:id="@+id/telenor_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/telenor_logo" />

        <ImageButton
            android:id="@+id/options"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="50dp"
            android:background="#00ACE7"
            android:src="@drawable/options" />

        <ImageButton
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="#00ACE7"
            android:src="@drawable/settings" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativelayout2"
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:background="#B6B6B4"
        android:visibility="visible"
        android:weightSum="1" 
        android:layout_below="@+android:id/relative_layout1" >
    </RelativeLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

在第二个相对布局

中将此属性更改为以下内容
android:layout_below="@id/relativeLayout1"

所以它最终看起来像这样

<RelativeLayout
    android:id="@+id/relativelayout2"
    android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:background="#B6B6B4"
    android:visibility="visible"
    android:weightSum="1" 
    android:layout_below="@id/relativeLayout1">
</RelativeLayout>

另外,快速说明一下:

android:layout_width="fill_parent"

(API级别8+)应替换为:

android:layout_width="match_parent"

答案 1 :(得分:0)

请用android:layout_below="@+android:id/relative_layout1"将此android:layout_below="@+id/relativeLayout1"更改为将relativleayout1设置为relativelayout2  Screen Shot

相关问题