【发布时间】: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