简体   繁体   中英

Set border for view with an icon in the corner

I would like to add a border to view which to have an icon in the corner like this:

在此处输入图像描述

I write a drawable with border:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@android:color/transparent" />
<stroke
    android:width="2dp"
    android:color="#F62626"
    android:dashWidth="3dp"
    android:dashGap="7dp" />

but I can't add icon in the corner. Thanks for every answers在此处输入图像描述 .

This is possible using layer-list, drawable and attributes like adroid:top, left, right, bottom. Something like this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_lock"
        android:top="-10dp"
        android:left="-10dp"/>
    <item
        android:left="10dp"
        android:top="10dp"
        android:bottom="10dp"
        android:right="10dp">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
        <solid android:color="@android:color/transparent" />
        <stroke
            android:width="2dp"
            android:color="#F62626"
            android:dashWidth="3dp"
            android:dashGap="7dp" />
        </shape>
    </item>
</layer-list>

The other way is to use ConstraintLayout and put there icons as separate views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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