我在CardView中有一个RelativeLayout,有一个ImageView和两个TextView。一个TextView必须居中,因此它放在ImageView上。另一个TextView必须在TextView下面,但由于某些原因我不知道它放在第一个TextView上。我设法通过添加第二个RelativeLayout并将两个TextView放在其中来正确设置它,现在它们正确显示。但我很好奇为什么如果你不添加第二个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>
这是因为ImageView
宽度为match_parent
。如果要在该表单中使用Image,则可以改为设置相对布局的背景。您可以为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
。
如果您在未匹配的XML中看到,您将找到用于ImageView的android:adjustViewBounds="true"
,其中此ImageView将被设置为调整其边界以保持其drawable的纵横比。
在上面的场景中,请尝试使用LinearLayout,将Directionation Vertical用于TextView,将RelativeLayout用于(LinearLayout和ImageView)。
constraintlayout布局始终是Ultimate Powerfull布局。