简体   繁体   English

android studio中文本视图的带背景图像的圆角

[英]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 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: Output:

Example Output:示例 Output:

As in official Android documentation for LayerList与 LayerList 的官方 Android 文档一样

A LayerDrawable is a drawable object that manages an array of other drawables. LayerDrawable 是一个可绘制的 object,它管理一系列其他可绘制对象。 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.这意味着您的可绘制对象需要放置在圆形背景之后才能可见。

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

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