Android custom widget styles: how to put them into a namespace?

前端 未结 4 1377
情话喂你
情话喂你 2021-01-01 17:17

In the ApiDemos, there is a view example called Gallery1 which declares a custom style in attrs.xml, as such:


            


        
相关标签:
4条回答
  • 2021-01-01 17:44

    create a xml file and paste this below code and put in res->values folder

    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
    

    and then copy below code

    TypedArray typedArray=this.obtainStyledAttributes(R.styleable.Gallery1);
            int back=typedArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            typedArray.recycle();
    

    and set in ur widget background i mean imageView.setBackgroundResource(back);

    0 讨论(0)
  • 2021-01-01 17:45

    I had a similar problem resulting in the error message No resource identifier found for attribute in package

    The solution for me was to declare the namespace when you use the custom attribute.

    In your xml file where you use your custom attribute specify:

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

    ...

    <gallery.widget.package.Gallery1
    
    
    myns:myCustomAttr="xxx"
    />
    
    0 讨论(0)
  • 2021-01-01 17:53

    In case someone is still interested, I had the same problem and solved it by adding a 'format' attribute (it seems it doesn't take 'string' as the default):

    <attr name="android:galleryItemBackground" format="integer"/>
    
    0 讨论(0)
  • 2021-01-01 18:04

    I found this article helpful in similar situation.

    "Referring to our new attributes is actually a two step process. First we declared a new namespace and next we specified the values of our new attributes in the XML usage."

    0 讨论(0)
提交回复
热议问题