格式属性值“android:drawable”无效
我正在尝试为我的按钮创建自定义属性,但我不知道在属性声明中必须使用哪种格式来创建图像格式属性值“android:drawable”无效,android,Android,我正在尝试为我的按钮创建自定义属性,但我不知道在属性声明中必须使用哪种格式来创建图像 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TCButton"> <attr name="Text" format="string"/> <attr name="BackgroundImage" format=
<?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>
错误为format=android:drawable…您可以使用format=integer、drawable的资源id和AttributeSet.getDrawable
这里有一个例子
在res/values/attrs.xml中将属性声明为整数:
<resources>
<declare-styleable name="MyLayout">
<attr name="icon" format="integer" />
</declare-styleable>
</resources>
要查看所有可能的选项,请检查,我认为最好将其用作简单的参考:
<declare-styleable name="TCButton">
<attr name="customText" format="string"/>
<attr name="backgroundImage" format="reference" />
</declare-styleable>
附言:
不要忘记为正在使用自定义视图的布局的根元素添加此行
xmlns:custom="http://schemas.android.com/apk/res-auto"
如果不设置此选项,您将无法访问自定义属性。从AOSP代码中,我了解了google工程师如何声明ImageViewsrc attr
上面的代码是一个示例,它可以涵盖我们开发中的大多数情况。再看一遍,可以补充一点,错误的名称空间声明不会产生编译时错误。在本例中,它可能看起来像xmlns:myapp=http://schemas.android.com/apk/res/se.jog.mob 如果在se.jog.mob.com中声明了MyLayout类,则在使用样式化属性时应调用a.recycle。在gradle项目中,自定义模式应始终为Yes,这里有一个关于弃用的问题:使用integer不允许我在XML中选择@drawable。我让你使用fromat=reference,然后它就工作了。你的例子中的自定义背景图像属性设置是错误的。更像:custom:backgroundImage=@drawable/myimage这是最好的答案。当您使用format=reference时,您将看到在custom:backgroundImage=@drawable/myImageFound上选择资源的下拉列表找到上述有用的向上投票。
<declare-styleable name="TCButton">
<attr name="customText" format="string"/>
<attr name="backgroundImage" format="reference" />
</declare-styleable>
<your.package.name.TCButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:customText="Some custom text"
custom:backgroundImage="@drawable/myImage"
/>
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);
}
}
xmlns:custom="http://schemas.android.com/apk/res-auto"
<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>