创建CardView,将其cornerRadius应用于其中的ImageView

时间:2018-08-17 08:33:03

标签: android material-design cardview

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>

2 个答案:

答案 0 :(得分: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">

答案 1 :(得分:0)

将单个视图(例如图像视图或文本视图)放在cardview内,并赋予它app:cardCornerRadius