Styling Custom Views

后端 未结 3 996
无人共我
无人共我 2021-02-04 15:47

I have a few custom views in my Android project and I\'ve added the relevant details to the attrs.xml file. And now I can implement my objects through XML. This works fine.

3条回答
  •  日久生厌
    2021-02-04 16:50

    For clarification, the item's name attribute should be the same as what is in the attrs.xml's declare-styleable name attribute + ":" + the attribute name.

    For example:

    attrs.xml:

    
      
        
            
         
    
    

    style.xml:

    
    
        
    
    

    You can then apply this style to all activities by using a theme in your manifest.xml file. Anywhere that a custom view exists that wants to use the "actionBarTextColor" attribute, you can then use the Java code:

    TypedArray typedArray = context.obtainStyledAttributes(attrSet, R.styleable.com_chuck_norris);
    COLOR_ACTION_BAR_TEXT = typedArray.getColor(R.styleable.com_chuck_norris_actionBarTextColor, 0xff0000ff);
    typedArray.recycle();
    

    I'm not sure why you cannot just define your schema in your style.xml file as was asked above, but it seems to be a limitation of style.xml.

提交回复
热议问题