简体   繁体   中英

Rounded corners with background image for text view in android studio

I have a text view and and I want its background to be a image and also its corners rounded. I have tried that by creating a drawable file but the background image gets spilled out.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:padding="5dp"
    android:orientation="horizontal">
    <TextView
    
android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/colorPrimaryDark"
        android:background="@drawable/drawable_file"
        />
</LinearLayout>

drawable_file.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@drawable/img" />
    </item>
    <item>
        <shape>
            <stroke
                android:width="1dp" />
           
            <corners android:radius="50dp" />
        </shape>
    </item>
</layer-list>

Output:

Example Output:

As in official Android documentation for LayerList

A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.

Meaning your drawable needs to be placed after the rounded background to be visible.

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