格式属性值“android:drawable”无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3487247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:26:01  来源:igfitidea点击:

Format attribute value "android:drawable" not valid

android

提问by Tiago Costa

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

错误是格式="android:drawable"...

回答by JOG

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

您可以使用format="integer"、drawable的资源 IDAttributeSet.getDrawable(...)

Here is an example.

这是一个例子。

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

在 res/values/attrs.xml 中将属性声明为整数:

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

Set the attribute to a drawable id in your layout:

在布局中将该属性设置为可绘制的 id:

<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);

To see all options possible check the android src here

要查看所有可能的选项,请在此处查看android src

回答by Ionut Negru

I think it will be better to use it as a simple reference:

我认为将其用作简单参考会更好:

<declare-styleable name="TCButton">
        <attr name="customText" format="string"/>
        <attr name="backgroundImage" format="reference"  />
</declare-styleable>

And set it in your xml like this:

并将其设置在您的 xml 中,如下所示:

<your.package.name.TCButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    custom:customText="Some custom text"
    custom:backgroundImage="@drawable/myImage"
/>

And in your class set the attributes like this:

并在您的班级中设置如下属性:

public TCButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MembershipItemView, 0, 0);

    String customText;
    Drawable backgroundImage;
    try {
        customText = a.getString(R.styleable.TCButton_customText);
        backgroundImage = a.getDrawable(R.styleable.TCButton_backgroundImage);
    } finally {
        a.recycle();
    }

    if(!TextUtils.isEmpty(customText)) {
      ((TextView)findViewById(R.id.yourTextView)).setText(customText);
    }

    if(null != backgroundImage) {                    
        ((ImageView)findViewById(R.id.yourImageView)).setBackgroundDrawable(backgroundImage);
    }
}

PS: Don't forget to add this line for the root element of the layout you are using your custom view in

PS:不要忘记为您使用自定义视图的布局的根元素添加这一行

xmlns:custom="http://schemas.android.com/apk/res-auto"

If you don't set this, you won't be able to access your custom attributes.

如果您不设置此项,您将无法访问您的自定义属性。

回答by CoXier

From AOSP code, I found how google engineers declare ImageView#srcattr.

从 AOSP 代码中,我发现了 google 工程师如何声明ImageView#srcattr。

<declare-styleable name="ImageView">
    <attr name="src" format="reference|color" />
    <attr name="scaleType">
        <enum name="matrix" value="0" />
        <enum name="fitXY" value="1" />
        <enum name="fitStart" value="2" />
        <enum name="fitCenter" value="3" />
        <enum name="fitEnd" value="4" />
        <enum name="center" value="5" />
        <enum name="centerCrop" value="6" />
        <enum name="centerInside" value="7" />
    </attr>
    <attr name="adjustViewBounds" format="boolean" />
    <attr name="maxWidth" format="dimension" />
    <attr name="maxHeight" format="dimension" />
    <attr name="tint" format="color" />
    <attr name="baselineAlignBottom" format="boolean" />
    <attr name="cropToPadding" format="boolean" />
    <attr name="baseline" format="dimension" />
    <attr name="drawableAlpha" format="integer" />
    <attr name="tintMode" />
</declare-styleable>

Above code is a sample and it can cover most case in our development.

上面的代码是一个示例,它可以覆盖我们开发中的大多数情况。