【问题标题】:layout:below not working in RelativeLayout inside CardView布局:下面在 CardView 内的 RelativeLayout 中不起作用
【发布时间】:2019-03-22 09:38:56
【问题描述】:

我在 CardView 中有一个 RelativeLayout,带有一个 ImageView 和两个 TextView。一个 TextView 必须居中,所以它被放置在 ImageView 上。另一个 TextView 必须低于该 TextView,但由于某种我不知道的原因,它位于第一个 TextView 之上。我设法通过添加第二个 RelativeLayout 并将两个 TextViews 放入其中来正确设置它,现在它们可以正确显示。但我很好奇为什么如果你不添加第二个RelativeLayout,第二个TextView 不会出现在第一个下面。有人可以解释一下吗?谢谢。

布局:

这种方式有效:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="10dp"
    app:cardUseCompatPadding="true"
    android:id="@+id/cv">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"/>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="40sp"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:textColor="@color/black"
                android:text="I am the name"
                android:layout_centerInParent="true" />

            <TextView
                android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:gravity="center"
                android:layout_centerHorizontal="true"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:text="I am the type"
                android:textColor="@color/red"
                android:textSize="25sp"
                android:textStyle="bold" />

        </RelativeLayout>

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


</RelativeLayout>

这不是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:elevation="10dp"
    app:cardUseCompatPadding="true"
    android:id="@+id/cv">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="40sp"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:textColor="@color/black"
                android:text="I am the name"
                android:layout_centerInParent="true" />

            <TextView
                android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:gravity="center"
                android:layout_centerHorizontal="true"
                android:shadowColor="#ffffff"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="6"
                android:text="I am the type"
                android:textColor="@color/red"
                android:textSize="25sp"
                android:textStyle="bold" />

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


</RelativeLayout>

【问题讨论】:

    标签: android android-cardview android-relativelayout


    【解决方案1】:

    这是因为ImageView 的宽度为match_parent。如果您想以该形式使用图像,则可以设置相对布局的背景。您可以为CardView 和任何其他设置设置高度。下面是代码。

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:elevation="10dp"
            app:cardUseCompatPadding="true"
            android:id="@+id/cv">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@mipmap/ic_launcher">
    
    
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/name"
                    android:gravity="center"
                    android:textStyle="bold"
                    android:textSize="40sp"
                    android:shadowColor="#ffffff"
                    android:shadowDx="0"
                    android:shadowDy="0"
                    android:shadowRadius="6"
                    android:textColor="#000"
                    android:text="I am the name"
                    android:layout_centerInParent="true" />
    
                <TextView
                    android:id="@+id/type"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/name"
                    android:gravity="center"
                    android:layout_centerHorizontal="true"
                    android:shadowColor="#ffffff"
                    android:shadowDx="0"
                    android:shadowDy="0"
                    android:shadowRadius="6"
                    android:text="I am the type"
                    android:textColor="#000"
                    android:textSize="25sp"
                    android:textStyle="bold" />
    
            </RelativeLayout>
        </android.support.v7.widget.CardView>
    
    
    </RelativeLayout>
    

    在其他情况下,如果您希望在布局中使用ImageView,您可以将RelativeLayout 宽度设置为match_parent,将ImageView 宽度设置为wrap_content。

    【讨论】:

    • 是的,这行得通。但我想知道的是,如果这三个元素(ImageView 和两个 TextViews)在同一个 RelativeLayout 内,我将第一个元素设置为他的父元素(RelativeLayout)的中心,第二个元素设置在下面第一个,它没有正确渲染。为什么会受 ImageView 中的 match_parent 影响?
    • 为什么将android:adjustViewBounds="true" 设置为ImageView?它的真正用途是保留其中使用的可绘制对象的纵横比。您可以删除它或删除相同的match_parent 一个。
    • 我想保留图片的纵横比...不是我的代码中的src设置的,顺便说一下,它是通过url和毕加索设置的。
    • 当您设置android:scaleType="fitXY" 或android:adjustViewBounds="true" 时,RelativeLayout 将相应地设置其中的项目,因此ImageView 的边界。
    • 但是TextViews的父级是RelativeLayout,而不是ImageView
    【解决方案2】:

    如果您在未匹配的 XML 中看到,您会发现 ImageView 的 android:adjustViewBounds="true",此 ImageView 将在其中设置以调整其边界以保持其可绘制对象的纵横比。

    在上述场景中,请尝试使用具有垂直方向的 LinearLayout 用于 TextView 和 RelativeLayout 用于(LinearLayout 和 ImageView)。

    constraintlayout 布局始终是 Ultimate Powerfull 布局。

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      相关资源
      最近更新 更多