android 里自定义属性

来源:互联网 发布:php 5.6.22 编译安装 编辑:程序博客网 时间:2024/05/19 16:02

Currently the best documentation is the source. You can take a look at it here (attrs.xml).

You can define attributes in the top <resources> element or inside of a <declare-styleable>element. If I'm going to use an attr in more than one place I put it in the root element. Note, all attributes share the same global namespace(android). That means that even if you create a new attribute inside of a<declare-styleable> element it can be used outside of it and you cannot create another attribute with the same name of a different type.

you can create another attribute with the same name of different type undering create a new namespace

eg. in android namespace you can :android:attr

     in self defined namespace  yournamespace:attr

An <attr> element has two xml attributes name and formatname lets you call it something and this is how you end up referring to it in code, e.g., R.attr.my_attribute. The format attribute can have different values depending on the 'type' of attribute you want.

  • reference - if it references another resource id (e.g, "@color/my_color", "@layout/my_layout")
  • color
  • boolean
  • dimension
  • float
  • integer
  • string
  • fraction
  • enum - normally implicitly defined
  • flag - normally implicitly defined
原创粉丝点击