The attribute value for the format "android: drawable" is invalid

I am trying to create custom attributes for my button, but I do not know what format I should use for images in the attribute 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 in format = "android: drawable" ...

+60
android
Aug. 15 '10 at 11:54
source share
3 answers

You can use format = "integer" , the resource identifier for drawing, and AttributeSet.getDrawable (...) .

Here is an example.

Declare an attribute as a whole in res / values ​​/attrs.xml:

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

Set the attribute for the drawable identifier in your layout:

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

Get the extract from the attribute in the 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 possible options check android src here

+146
May 24 '11 at 9:09 a.m.
source share

I think it is 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 install it in xml as follows:

 <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 these attributes:

 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: Remember to add this line to the root element of the layout that you are using in the custom view in

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

If you do not set this, you will not be able to access your user attributes.

+32
Aug 6 '14 at 10:50
source share

From the AOSP code, I learned how Google engineers declare ImageView#src attr.

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

The code above is an example, and it can cover most cases in our development.

+6
Jul 13 '18 at 7:06
source share



All Articles