Create a custom Drawable and use it in XML

≡放荡痞女 提交于 2019-12-03 01:31:12

Unfortunately it is not possible to use custom drawables in XML files (for API 23 and below) due to potential security issues. See here for a Google Groups thread about this very topic with a response from Romain Guy, one of the Android developers.

You can still use them in your Java code though.

Update:

As LeoFarage points out in the comments, starting in API 24 you can now use custom drawables in XML files but only within your application package.

Starting Android API 24, custom Drawables classes can be used in XML only within your package.

Custom drawables classes may be used in XML in multiple ways:

Using the fully-qualified class name as the XML element name. For this method, the custom drawable class must be a public top-level class.

<com.yourapp.MyCustomDrawable
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:color="#ffff0000"/>

Using drawable as the XML element name and specifying the fully-qualified class name from the class attribute. This method may be used for both public top-level classes and public static inner classes.

<drawable xmlns:android="http://schemas.android.com/apk/res/android"
 class="com.myapp.MyTopLevelClass$InnerCustomDrawable"
 android:color="#ffff0000" />

Note: that is not supported by support library.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!