Android 可绘制资源中带有形状的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23299552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text with shapes in drawable resource
提问by Mykhailo Yuzheka
Can i create text-shape in drawable resource? I was googling much but found nothing... Here is my drawable file:
我可以在可绘制资源中创建文本形状吗?我在谷歌上搜索了很多,但一无所获......这是我的可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="3dp" android:color="#QQffQQ"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
<item android:right="59dp" android:left="59dp">
<shape android:shape="rectangle">
<solid android:color="£22££20"/>
</shape>
</item>
<item android:top="59dp" android:bottom="59dp">
<shape android:shape="rectangle">
<solid android:color="£20££20"/>
</shape>
</item>
<item>
<!--text should be here-->
</item>
</layer-list>
采纳答案by BVB
No, you cannot do so. One idea is to set the Drawable
as the background for a TextView
and then simply set text in the TextView
, which will appear above the other layers of your Drawable
. Of course, this cannot be used for a splash screen, which requires a drawable resource as mentioned by zyamysin a comment below. An idea for that case is to generate static images with the text you are interested in. This unfortunately does not cover cases where the text is to be dynamic.
不,你不能这样做。一种想法是将 设置Drawable
为 a 的背景TextView
,然后简单地在 中设置文本,该文本TextView
将出现在Drawable
. 当然,这不能用于启动画面,这需要zyamys在下面的评论中提到的可绘制资源。这种情况的一个想法是使用您感兴趣的文本生成静态图像。不幸的是,这不包括文本是动态的情况。
回答by Leo Droidcoder
You can use vector drawable instead (say by converting from svg file).
Then use vector as one of the layers.
This lets you create a single drawable without any TextViews, so you can easily use it as a windowBackground in your splash screen theme.
您可以使用 vector drawable 代替(例如通过从 svg 文件转换)。
然后使用矢量作为图层之一。
这使您可以创建一个没有任何 TextViews 的可绘制对象,因此您可以轻松地将其用作初始屏幕主题中的 windowBackground。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:angle="270"
android:startColor="#C3DAE0"
android:endColor="#FFFFFF"
android:type="linear"/>
</shape>
</item>
<item
android:gravity="center"
android:drawable="@drawable/ic_splash_text"/>
</layer-list>
Where ic_splash_text- is a vector drawable with the text.
其中ic_splash_text- 是可随文本绘制的矢量。
Not forget to add vectors support if you are4 targeting onto API<21. For this you have to:
如果您的目标是 API<21,请不要忘记添加向量支持。为此,您必须:
Add to your module build.gradle (app-level):
android { vectorDrawables.useSupportLibrary = true. }
Register delegate in a static block of your activity:
static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); }
添加到您的模块 build.gradle(应用程序级):
android { vectorDrawables.useSupportLibrary = true. }
在活动的静态块中注册委托:
static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); }