【问题标题】:Create CardView applying its cornerRadius to ImageView inside it创建 CardView,将其cornerRadius 应用到其中的 ImageView
【发布时间】:2018-08-17 16:42:21
【问题描述】:

https://github.com/googlecodelabs/android-design-library/blob/master/app/src/main/res/layout/item_card.xml#L17

我尝试在 Android 应用程序中使用带有角半径的 ImageView 创建 CardView。

我发现当我们尝试将 cardCornerRadius 设置为超过 0 时,例如 12dp,它没有将半径应用于 CardView 内部的 ImageView。

如何将半径应用于 CardView 内的 ImageView? 提前感谢您的帮助。

created image with app:cardCornerRadius="0dp"

created image with app:cardCornerRadius="12dp"

<android.support.v7.widget.CardView 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:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="@dimen/card_height"
android:layout_gravity="center"
android:layout_marginBottom="@dimen/md_keylines"
android:layout_marginLeft="@dimen/md_keylines"
android:layout_marginRight="@dimen/md_keylines"
app:cardCornerRadius="12dp" // This radius is not applied to the ImageView with @id/card_image
android:foreground="?attr/selectableItemBackground">

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

    <ImageView
        android:id="@+id/card_image"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_image_height"
        android:scaleType="centerCrop"
        tools:src="@drawable/a" />

    <TextView
        android:id="@+id/card_title"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_title_height"
        android:layout_alignBottom="@+id/card_image"
        android:layout_marginLeft="@dimen/md_keylines"
        tools:text="@string/item_title"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/card_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_image"
        android:layout_marginLeft="@dimen/md_keylines"
        android:layout_marginTop="@dimen/md_keylines"
        android:layout_marginBottom="@dimen/md_keylines"
        android:layout_marginRight="@dimen/md_keylines"
        tools:text="@string/item_desc"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="@color/dark_grey"
        android:textSize="@dimen/article_subheading" />

    <Button
        android:id="@+id/action_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_text"
        style="?android:attr/borderlessButtonStyle"
        android:textColor="?attr/colorPrimary"
        android:text="Action" />

    <ImageButton
        android:id="@+id/share_button"
        android:layout_width="@dimen/cards_button_width"
        android:layout_height="@dimen/cards_button_height"
        android:layout_marginRight="@dimen/md_keylines"
        app:srcCompat="@drawable/ic_share"
        android:layout_below="@+id/card_text"
        android:layout_alignParentRight="true"
        style="?android:attr/borderlessButtonStyle"
        android:tint="@color/button_grey" />

    <ImageButton
        android:id="@+id/favorite_button"
        android:layout_width="@dimen/cards_button_width"
        android:layout_height="@dimen/cards_button_height"
        android:layout_marginRight="@dimen/md_keylines"
        app:srcCompat="@drawable/ic_favorite"
        android:layout_below="@+id/card_text"
        android:layout_toLeftOf="@id/share_button"
        style="?android:attr/borderlessButtonStyle"
        android:tint="@color/button_grey" />
</RelativeLayout>

【问题讨论】:

  • 它只发生在 API

标签: android material-design cardview


【解决方案1】:

使用app:cardUseCompatPadding="true"

<android.support.v7.widget.CardView 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:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/card_height"
    android:layout_gravity="center"
    android:layout_marginBottom="@dimen/md_keylines"
    android:layout_marginLeft="@dimen/md_keylines"
    android:layout_marginRight="@dimen/md_keylines"
    android:foreground="?attr/selectableItemBackground"
    app:cardCornerRadius="12dp"
    app:cardUseCompatPadding="true">

【讨论】:

    【解决方案2】:

    将单个视图(例如图像视图或文本视图)放入卡片视图中并赋予它 app:cardCornerRadius

    【讨论】:

    • 谢谢。您的意思是我们需要添加子 CardView 并将 ImageView 放入其中对吗?是的,我预计它会起作用,但实际上并没有......@SinaMN75
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2019-11-16
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多