首页 > 解决方案 > 相对布局中的 layout_below 不会显示

问题描述

我正在尝试在相对布局中的 cardview 下创建一个 layout_below,但它不会显示在我的 layout_above 工作设备中。我不知道为什么它不起作用。我认为它应该可以工作,因为它低于卡片视图。请帮助我错过了什么或错了什么。

这是我的 xml 代码

<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:background="@drawable/background_2"
    tools:context=".ExchangeFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/saldo"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/holder" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center_horizontal"
            android:text="Will Stitch"
            android:textSize="15dp"
            android:textStyle="bold" />
    </LinearLayout>

    <android.support.v7.widget.CardView
        android:id="@+id/saldo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Saldo"
                android:textColor="@color/colorPrimary"
                android:textSize="20dp"
                android:textStyle="bold" />

    </android.support.v7.widget.CardView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/saldo"
        android:orientation="vertical">

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="WillStitch@gmail.com"
                android:textSize="10dp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

请帮助为什么会这样

标签: androidandroid-layout

解决方案


它正在工作,但是您ImageView占用了所有宽度并且它没有android:src属性(因此它什么都不显示)并且您不需要最后一个 LinearLayout :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/saldo"
    android:orientation="vertical">

    <!-- Useless Linear Layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="WillStitch@gmail.com"
            android:textSize="10dp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

推荐阅读