Android Studio ImageView centerCardView中的裁剪溢出

bxfogqkk  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(198)

我在做我的日常练习。我看到了这个问题,图像不适合Cardview。
我创建了一个Cardview,
"看起来像这样"

代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:cardCornerRadius="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_gradient_hydroponic"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="15dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Types of Hydroponics"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="You can find out the various hydroponic systems, from how they work, the media needed and planting to making them."
                android:textColor="@color/white" />

        </LinearLayout>

        <ImageView
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_margin="15dp"
            android:layout_gravity="bottom"
            android:background="@drawable/shape_myplants"
            android:src="@drawable/ic_baseline_navigate_next_24"/>

    </LinearLayout>

</androidx.cardview.widget.CardView>

然后,当我把背景图像,放在这里的时候,问题就从这里开始了,我已经把,放在这里了,这里有一个缺口,你们可以看到。
"这里看起来像,"

代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:cardCornerRadius="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_gradient_hydroponic">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bg_card"
            android:scaleType="centerCrop"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_margin="15dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Types of Hydroponics"
                    android:textColor="@color/white"
                    android:textSize="20sp"
                    android:textStyle="bold"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="You can find out the various hydroponic systems, from how they work, the media needed and planting to making them."
                    android:textColor="@color/white" />

            </LinearLayout>

            <ImageView
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_margin="15dp"
                android:layout_gravity="bottom"
                android:background="@drawable/shape_myplants"
                android:src="@drawable/ic_baseline_navigate_next_24"/>

        </LinearLayout>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

这是我的最终解决方案,我将ImageView设置为android:layout_height="150dp"

代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:cardCornerRadius="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_gradient_hydroponic">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:src="@drawable/bg_card"
            android:scaleType="centerCrop"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_margin="15dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Types of Hydroponics"
                    android:textColor="@color/white"
                    android:textSize="20sp"
                    android:textStyle="bold"/>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="You can find out the various hydroponic systems, from how they work, the media needed and planting to making them."
                    android:textColor="@color/white" />

            </LinearLayout>

            <ImageView
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_margin="15dp"
                android:layout_gravity="bottom"
                android:background="@drawable/shape_myplants"
                android:src="@drawable/ic_baseline_navigate_next_24"/>

        </LinearLayout>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

那么,我如何才能使我的ImageView与第一个的CardView高度相匹配呢?(图1)。我只是很好奇。

anauzrmj

anauzrmj1#

好了,完成了!我反复试验,得到了以下结果。这是一个响应视图。我将Imageview的id拖到height wrap_content布局中。它将遵循wrap_content布局的高度。
只需在您ImageView android:layout_alignBottom="@+id/height_card"上使用此功能

以下是一个示例:

以下是代码:

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:cardCornerRadius="10dp">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/height_card"
            android:scaleType="centerCrop"
            android:src="@drawable/shape_gradient_hydroponic" />

        <LinearLayout
            android:id="@+id/height_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_margin="15dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Types of Hydroponics"
                    android:textColor="@color/white"
                    android:textSize="20sp"
                    android:textStyle="bold"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="You can find out the various hydroponic systems, from how they work, the media needed and planting to making them."
                    android:textColor="@color/white" />

            </LinearLayout>

            <ImageView
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_margin="15dp"
                android:layout_gravity="bottom"
                android:background="@drawable/shape_myplants"
                app:tint="#217373"
                android:src="@drawable/ic_baseline_navigate_next_24"/>

        </LinearLayout>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

相关问题