简体   繁体   English

如何将attr放入自定义可绘制对象中?

[英]How to put an attr inside a custom drawable?

I have an issue when trying to put an attr inside a custom drawable xml. 尝试将attr放在自定义可绘制xml中时遇到问题。

Here's a sample of my layout: 这是我的布局示例:

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/my_custom_btn"/>

Then my custom drawable " my_custom_btn ", I want to specify its color with attr_my_custom_color: 然后我的自定义可绘制对象“ my_custom_btn ”,我想使用attr_my_custom_color指定其颜色:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"
            android:drawable="?attr/attr_my_custom_color" /> <!-- pressed -->
    </selector>

My attr.xml file: 我的attr.xml文件:

<resources>
     <attr name="attr_my_custom_color" format="color"/>
</resources>

And my style.xml file, my application is using this theme (in my manifest): 还有我的style.xml文件,我的应用程序正在使用此主题(在清单中):

<resources>
<style name="AppThemeTest" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="attr_my_custom_color">@color/primary</item>

</style>

My attr works fine when I test it in my layout, but when I put it inside my custom drawable, it doesn't work. 当我在布局中对其进行测试时,我的attr可以正常工作,但是当我将其放入自定义可绘制对象中时,它将无法正常工作。

It compiles fine, but when I launch my app, it displays this error: 编译正常,但是当我启动我的应用程序时,它显示此错误:

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: tag requires a 'drawable' attribute or child tag defining a drawable 引起原因:org.xmlpull.v1.XmlPullParserException:二进制XML文件第4行:标记需要一个'drawable'属性或定义可绘制对象的子标记

Can you help me to specify an attr inside a custom drawable? 您能帮我在自定义可绘制对象中指定attr吗?

you have to change background to drawable


<Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawable ="@drawable/my_custom_btn"/>

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

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