2

I know I can use a LinearLayout to do this, but I have to use a RelativeLayout because I have another RelativeLayout that I need to anchor to the bottom of the overall layout, as sort of a "status" bar, and I want the title and text to be above the status bar, aligned to the top. Using a RelativeLayout was the only way I could get the status bar to work. The only problem is the title and text TextViews overlap each other and I need the text to be immediately below the title:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
         >       

            <RelativeLayout
                android:id="@+id/status" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_alignParentBottom="true"
                android:paddingTop="10dp"
              >
                    <TextView 
                        android:id="@+id/status" 
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_alignParentLeft="true" 
                    />      
            </RelativeLayout>

            <TextView 
                android:id="@+id/title" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_above="@id/status"       
                android:layout_alignParentTop="true"
            />

            <TextView 
                android:id="@+id/text" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"    
                android:layout_alignParentTop="true"    
            />

    </RelativeLayout>

</FrameLayout>
4

3 回答 3

2

您可以将 LinearLayout 用作 RelativeLayout 的子级。

   <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true">     
        <TextView 
            android:id="@+id/title" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />
        <TextView 
            android:id="@+id/text" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />
  </LinearLayout>
于 2012-07-10T03:43:38.487 回答
0

XML 布局是按照它们“读取”的顺序呈现的。所以你会想要做这样的事情:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
         >       

           <TextView 
                android:id="@+id/title" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_above="@id/status"       
                android:layout_alignParentTop="true"
            />

            <TextView 
                android:id="@+id/text" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"    
                android:layout_alignParentTop="true"    
            />

            <RelativeLayout
                android:id="@+id/status" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_alignParentBottom="true"
                android:paddingTop="10dp"
              >
                    <TextView 
                        android:id="@+id/status" 
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_alignParentLeft="true" 
                    />      
            </RelativeLayout>



    </RelativeLayout>

</FrameLayout>

我也会像其他人提到的那样将两个 textViews 包装在一个 linearLayout 中。这会对你有很大帮助。

希望有帮助。

于 2012-07-10T04:03:44.580 回答
0

android:layout_alignParentTop="true"从您的文本 TextView 中删除。那会解决你的问题!

于 2013-03-16T13:48:23.247 回答