Text with shapes in drawable resource

2019-04-19 19:25发布

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>

2条回答
何必那么认真
2楼-- · 2019-04-19 20:09

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.

<?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.

Not forget to add vectors support if you are4 targeting onto API<21. For this you have to:

  1. Add to your module build.gradle (app-level):

    android {        
        vectorDrawables.useSupportLibrary = true.    
    }  
    
  2. Register delegate in a static block of your activity:

    static {  
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);   
    }    
    
查看更多
聊天终结者
3楼-- · 2019-04-19 20:15

No, you cannot do so. However, why not 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?

查看更多
登录 后发表回答