Android、layout_above 不工作

Android, layout_above not working

我想在屏幕中间、页眉下方和页脚上方显示一些文本。由于此文本很长,我将其嵌套在 ScrollView 中。我已经尝试了很多解决方案:this, this, this, this,还有更多...

起初我遇到了两个 relative_layouts 重叠和文本被剪切的问题。最被接受的答案是使用 layout_abovelayout_below,但是 当我使用 layout_above 时,文本永远不会显示 .

这是我的 xml 的样子:

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

<RelativeLayout
    android:id="@+id/linear_layout_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:padding="5dp"
    android:weightSum="3">

        <ImageView
            android:id="@+id/profile_image"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:contentDescription="@string/profile_photo"
            android:src="@drawable/default_profile"
            tools:ignore="RtlHardcoded" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/scrollViewAndStuff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/ln_layout_footer"
    android:layout_below="@+id/linear_layout_header">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        tools:ignore="UselessParent">

        <TextView
            android:id="@+id/meetupDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="@string/text"/>-
    </ScrollView>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/ln_layout_footer"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="horizontal"
    android:paddingTop="10dp">

    <LinearLayout
        android:id="@+id/ln_layout"
        android:layout_width="fill_parent"
        android:background="@color/gray"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp">

        <TextView
            android:id="@+id/red"
            android:textColor="@color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/red" />

        <TextView
            android:id="@+id/blue"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/white"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/blue" />

        <TextView
            android:id="@+id/green"
            android:layout_width="match_parent"
            android:textColor="@color/white"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
        android:text="@string/green" />
    </LinearLayout>

</RelativeLayout>

在你的 ln_layout_footer

设置android:layout_height="wrap_content"


已编辑

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/linear_layout_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/dark_gray_pressed"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:padding="5dp"
    android:weightSum="3">

    <ImageView
        android:id="@+id/profile_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_margin="10dp"
        android:contentDescription="@string/action_settings"
        android:src="@drawable/img_splash_logo"
        tools:ignore="RtlHardcoded" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/scrollViewAndStuff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ln_layout_footer"
    android:layout_below="@+id/linear_layout_header">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        tools:ignore="UselessParent">

        <TextView
            android:id="@+id/meetupDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="@string/txt_price" />-
    </ScrollView>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/ln_layout_footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:orientation="horizontal"
    android:paddingTop="10dp">

    <LinearLayout
        android:id="@+id/ln_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/dark_gray"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp">

        <TextView
            android:id="@+id/red"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/txt_quantity"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/blue"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/txt_material"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/green"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/msg_enter_user_name"
            android:textColor="@color/white" />
    </LinearLayout>

</RelativeLayout>

在您使用两个属性进行布局时,这就是为什么它不起作用的原因。所以你可以删除一个属性这里是你的工作代码

    <RelativeLayout
    android:id="@+id/scrollViewAndStuff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linear_layout_header">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp">

        <TextView
            android:textColor="#000"
            android:id="@+id/meetupDescription"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:text="@string/text" />-
    </ScrollView>

</RelativeLayout>

您可以使用这个 android:layout_below="@+id/linear_layout_header 或这个 android:layout_above="@+id/ln_layout_footer,一次一个 属性。或者简单地复制并过去