如何在 CardView 的右上角放置一个图标?

How do I place an icon on top-right position of CardView?

它以更小的宽度改变它的位置。

我希望它是所有屏幕的标准。

您可以在下方尝试 code.change 您在 imageView 中的 src 文件

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="@dimen/_100sdp"
    android:layout_margin="@dimen/_15sdp"
    app:cardBackgroundColor="@color/com_facebook_blue"
    app:cardCornerRadius="@dimen/_15sdp"
    app:cardElevation="@dimen/_5sdp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_margin="10dp"
                android:src="@drawable/ic_checkbox_selected" />
        </RelativeLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

希望对您有所帮助!

谢谢。

CardView 是从 FrameLayout 继承的,所以你可以做这样的东西:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|end"
    android:layout_margin="@dimen/margin_normal"
    android:src="@drawable/ic_warning" />

但为了更加准确和灵活,我建议使用此变体:

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/margin_normal"
        android:src="@drawable/ic_warning" />

</RelativeLayout>