макет: ниже не работает в RelativeLayout внутри CardView - PullRequest
0 голосов
/ 22 марта 2019

У меня есть RelativeLayout внутри CardView, с 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>

Ответы [ 2 ]

0 голосов
/ 22 марта 2019

Если вы видите в XML с пропущенным соответствием, вы найдете android:adjustViewBounds="true" для ImageView, где этот ImageView будет настроен так, чтобы корректировать свои границы, чтобы сохранить соотношение сторон его отрисовки.

в приведенном выше сценарии, пожалуйста, попробуйте использовать LinearLayout с вертикальной ориентацией для TextView и RelativeLayout для (LinearLayout и ImageView).

макет ограничения ограничения всегда является макетом Ultimate Powerfull.

0 голосов
/ 22 марта 2019

Это из-за 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 width для match_parent и ImageView width для wrap_content.

...