Android Материал CardView с дочерним клипом ConstraintLayout не работает - PullRequest
0 голосов
/ 17 марта 2020

Я хочу перекрыть (взять половину изображения снаружи) вида карты. ShapeableImageView является дочерним элементом представления карты.

Я попытался добавить clipChilder="false" в представлении карты, а также в макете дочернего ограничения, но безуспешно Я тоже попробовал то же самое во время выполнения снова, не повезло.

Макет

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.ProfileFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.codepaints.velvet.views.HeaderView
            android:id="@+id/v_profile_header"
            android:layout_width="match_parent"
            android:layout_height="235dp"
            android:src="@drawable/img_profile_bg"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:overlay_color="@color/colorNavHeaderOverlay" />

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/v_profile_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/app_space"
            android:layout_marginEnd="@dimen/app_space"
            android:clickable="false"
            android:focusable="false"
            android:translationY="-24dp"
            app:cardCornerRadius="@dimen/app_space_h"
            app:cardElevation="@dimen/app_shadow_two"
            app:layout_constraintTop_toBottomOf="@+id/v_profile_header"
            app:layout_constraintBottom_toBottomOf="@id/v_profile_header"
            app:layout_constraintEnd_toEndOf="@+id/v_profile_header"
            app:layout_constraintStart_toStartOf="@id/v_profile_header"
            app:rippleColor="@android:color/transparent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/v_profile_card_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false">

                <com.google.android.material.imageview.ShapeableImageView
                    android:id="@+id/v_profile_avatar"
                    android:layout_width="@dimen/profile_avatar_size"
                    android:layout_height="@dimen/profile_avatar_size"
                    android:background="@drawable/shape_circle_border"
                    android:src="@drawable/img_profile_avatar"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toTopOf="parent"/>

                <View
                    android:id="@+id/v_profile_online_status_holder"
                    android:layout_width="@dimen/app_space"
                    android:layout_height="@dimen/app_space"
                    app:layout_constraintEnd_toEndOf="@id/v_profile_avatar"
                    app:layout_constraintStart_toStartOf="@id/v_profile_avatar"
                    app:layout_constraintTop_toBottomOf="@+id/v_profile_avatar" />

                <View
                    android:id="@+id/v_profile_online_status"
                    android:layout_width="@dimen/app_space"
                    android:layout_height="@dimen/app_space"
                    android:layout_marginBottom="@dimen/app_space_h"
                    android:background="@drawable/shape_circle"
                    android:backgroundTint="@color/colorOnline"
                    app:layout_constraintBottom_toBottomOf="@id/v_profile_online_status_holder"
                    app:layout_constraintEnd_toEndOf="@id/v_profile_avatar"
                    app:layout_constraintStart_toStartOf="@id/v_profile_avatar" />

                <TextView
                    android:id="@+id/v_profile_username"
                    style="@style/TextAppearance.MaterialComponents.Headline6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/profile_username"
                    android:textAlignment="center"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/v_profile_avatar" />

                <TextView
                    android:id="@+id/v_profile_qualification"
                    style="@style/TextAppearance.MaterialComponents.Body2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/ic_university"
                    android:drawablePadding="@dimen/app_space_q"
                    android:text="@string/profile_qualification"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/v_profile_username" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </com.google.android.material.card.MaterialCardView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

Скриншот

enter image description here

Другой Тест

Я пробовал без MaterialCardView, но результат тот же. Понятия не имею, почему он не работает.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.ProfileFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/v_profile_card"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:clipChildren="false"
        app:elevation="1dp"
        android:clipToPadding="false">

        <ImageView
            app:elevation="16dp"
            android:id="@+id/v_image"
            android:layout_width="128dp"
            android:layout_height="128dp"
            android:src="@drawable/img_profile_avatar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="parent"/>

        <TextView
            android:id="@+id/v_user"
            style="@style/TextAppearance.MaterialComponents.Headline4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/profile_username"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/v_image"
            android:background="@color/colorNutritionKcal"
            android:translationY="60dp"
            android:elevation="100dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Снимок экрана

enter image description here

Тест 3

Он также не будет работать с LinearLayout: (

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/v_profile_card"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/colorAccent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:clipChildren="false"
    android:elevation="1dp"
    android:clipToPadding="false">

    <ImageView
        android:elevation="@dimen/app_space"
        android:id="@+id/v_image"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:translationY="-60dp"
        android:src="@drawable/img_profile_avatar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="parent"/>

    <TextView
        android:id="@+id/v_user"
        style="@style/TextAppearance.MaterialComponents.Headline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/profile_username"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/v_image"
        android:background="@color/colorNutritionKcal"
        android:translationY="60dp"
        android:elevation="100dp"/>

</LinearLayout>

1 Ответ

0 голосов
/ 20 марта 2020

Хорошо, так что после того, как мы попробовали так много разных способов, наконец-то нашли решение.

В моем случае у меня есть изображение, обернутое представлением карты, и представление карты находится внутри схемы ограничения. Так что здесь я должен установить android:clipChildren="false" для обоих представлений, ConstraintLayout родитель и CardView дочерний. Смотрите окончательный код ниже.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.ProfileFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false">

        <com.codepaints.velvet.views.HeaderView
            android:id="@+id/v_profile_header"
            android:layout_width="match_parent"
            android:layout_height="235dp"
            android:src="@drawable/img_profile_bg"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:overlay_color="@color/colorNavHeaderOverlay" />

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/v_profile_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/app_space"
            android:layout_marginEnd="@dimen/app_space"
            android:clickable="false"
            android:focusable="false"
            android:translationY="-24dp"
            android:clipChildren="false"
            app:cardCornerRadius="@dimen/app_space_h"
            app:cardElevation="@dimen/app_shadow_two"
            app:layout_constraintTop_toBottomOf="@+id/v_profile_header"
            app:layout_constraintBottom_toBottomOf="@id/v_profile_header"
            app:layout_constraintEnd_toEndOf="@+id/v_profile_header"
            app:layout_constraintStart_toStartOf="@id/v_profile_header"
            app:rippleColor="@android:color/transparent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/v_profile_card_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.imageview.ShapeableImageView
                    android:id="@+id/v_profile_avatar"
                    android:layout_width="@dimen/profile_avatar_size"
                    android:layout_height="@dimen/profile_avatar_size"
                    android:background="@drawable/shape_circle_border"
                    android:src="@drawable/img_profile_avatar"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toTopOf="parent"/>

                <View
                    android:id="@+id/v_profile_online_status_holder"
                    android:layout_width="@dimen/app_space"
                    android:layout_height="@dimen/app_space"
                    app:layout_constraintEnd_toEndOf="@id/v_profile_avatar"
                    app:layout_constraintStart_toStartOf="@id/v_profile_avatar"
                    app:layout_constraintTop_toBottomOf="@+id/v_profile_avatar" />

                <View
                    android:id="@+id/v_profile_online_status"
                    android:layout_width="@dimen/app_space"
                    android:layout_height="@dimen/app_space"
                    android:layout_marginBottom="@dimen/app_space_h"
                    android:background="@drawable/shape_circle"
                    android:backgroundTint="@color/colorOnline"
                    app:layout_constraintBottom_toBottomOf="@id/v_profile_online_status_holder"
                    app:layout_constraintEnd_toEndOf="@id/v_profile_avatar"
                    app:layout_constraintStart_toStartOf="@id/v_profile_avatar" />

                <TextView
                    android:id="@+id/v_profile_username"
                    style="@style/TextAppearance.MaterialComponents.Headline6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/profile_username"
                    android:textAlignment="center"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/v_profile_avatar" />

                <TextView
                    android:id="@+id/v_profile_qualification"
                    style="@style/TextAppearance.MaterialComponents.Body2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/ic_university"
                    android:drawablePadding="@dimen/app_space_q"
                    android:text="@string/profile_qualification"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/v_profile_username" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </com.google.android.material.card.MaterialCardView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>
...