Create a custom Drawable and use it in XML

限于喜欢 提交于 2019-12-20 11:31:54

问题


I am busy creating a less complicated version of NinePatchDrawable that tiles the inner panels instead of stretching them.

I can implement the actual logic to do the rendering and such.

However, I can't find documentation about how to use this Drawable in the XML files. Is this possible? That is, can I have a <tileable-nine-patch>, or some such, tag I can use to define resources?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9019044/create-a-custom-drawable-and-use-it-in-xml

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