繁体   English   中英

将文本添加到android drawable xml

[英]Add text to android drawable xml

我正在尝试使用 android drawable xml。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <item android:drawable="@color/colorPrimary"/>
    <item>
        <bitmap
            android:src="@drawable/ic_logo_black"
            android:gravity="center"/>
    </item>
    <Text is it possible to insert some text here???/>
</layer-list>

出于品牌目的,我想在此 xml 中插入一些文本。 (如果文本被翻译,我什至不重要。我只是想要它,以便每当我使用这个可绘制的 xml 时,在我指定的区域中插入一些文本)

这甚至可能吗? 我真的可以在 drawable.xml 中放置文本/字符吗? 是我修改图像并以这种方式添加文本的唯一方法吗?

谢谢

不幸的是,你不能那样做,更多请参考这里。

是的,您可以,如果您只想在按钮内放置一些文本,请使用背景设置为可绘制对象的 TextView。

可绘制资源

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/action_map_icon_on_v2" android:state_pressed="true"/>
  <item android:drawable="@drawable/action_map_icon_on_v2" android:state_selected="true"/>
  <item android:drawable="@drawable/action_map_icon_off_v2"/>
</selector>

布局文件

<TextView
            android:id="@+id/action_map"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingTop="5dp"
            android:textSize="12sp"
            android:textStyle="bold"
            android:textAlignment="center"
            android:text="MAP"
            android:background="@drawable/action_map_button"
/>

这家伙显然设法用文本创建了一个可绘制的矢量。 但我不知道如何:

如何访问或翻译 Vector Drawables 中的文本

也许他使用Inkscape 选项将文本转换为 path 明天我会更深入地研究它。

嗨,这个链接可能会帮助你

https://stackoverflow.com/a/60523334/6494118

无论您在何处尝试添加 Drawable,都可以使用此方法。

private Drawable getCircleDrawableWithText(Context context, String string) {

    Drawable background = ContextCompat.getDrawable(context, R.drawable.circle_yellow);
    Drawable text = CalendarUtils.getDrawableText(context, string, null, android.R.color.white, 12);
    Drawable[] layers = {background, text};

    return new LayerDrawable(layers);
}

形状或背景的资源文件您可以使用以下代码。 您可以用不同的形状替换它。 可绘制文件 circle_yellow 将在下面给出圆形的形状。

     <?xml version="1.0" encoding="utf-8"?>
     <shape
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="oval">

     <solid
         android:color="@color/yellow"/>

     <size
       android:width="120dp"
        android:height="120dp"/>
     </shape>

暂无
暂无

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

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