格式属性值QUOT;机器人:可绘制"无效机器人、属性、格式、QUOT

2023-09-04 03:25:15 作者:☆往事随風☆

我想创建自定义属性,以我的按钮,但我不知道哪种格式我必须使用图像属性的声明...

I'm trying to create custom attributes to my button but I dont know which format I must use to images in attributes declaration...

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="TCButton">
        <attr name="Text" format="string"/>
        <attr name="BackgroundImage" format="android:drawable"  />
    </declare-styleable>


</resources>

错误的格式为=机器人:可绘制......

Error is in the format="android:drawable"...

推荐答案

您可以使用格式=整数,在资源ID 提拉的,和 AttributeSet.getDrawable(...)

You can use format="integer", the resource id of the drawable, and AttributeSet.getDrawable(...).

下面是一个例子。

声明的属性,如RES /价值/ attrs.xml整数:

Declare the attribute as integer in res/values/attrs.xml:

<resources>
    <declare-styleable name="MyLayout">
        <attr name="icon" format="integer" />
    </declare-styleable>
</resources>

属性设置为一个可绘制的ID在你的布局:

Set the attribute to a drawable id in your layout:

<se.jog.MyLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    myapp:icon="@drawable/myImage"
/>

获取从属性的绘制在自定义窗口小部件组件类:

Get the drawable from the attribute in your custom widget component class:

ImageView myIcon;
//...
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyLayout);
Drawable drawable = a.getDrawable(R.styleable.MyLayout_icon);
if (drawable != null)
    myIcon.setBackgroundDrawable(drawable);

要查看所有可能的选项检查android SRC这里

To see all options possible check the android src here