繁体   English   中英

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

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

它用更小的宽度改变了它的 position。

我希望它适用于所有屏幕。

您可以尝试以下代码。更改 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>

我希望这可以帮助你!

谢谢你。

您可以使用此代码

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>

</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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM