您的位置:首页 > 移动开发 > Android开发

Android中的style,attr,theme

2012-12-27 15:00 405 查看
总结下,免得每次忘

1)style类似于css,用来定义View等UI元素的属性,比如layout_width等,可以在layout的xml中用@style/name来引用

2)style中如果要加入自定义属性,可以在attr中定义,一个attr包括name和format属性,可以在style中用@attr/name来引用

      也可以在attr中定义属性集合,用declare-styleable标签定义,这个属性集合在view的构造函数中用到

      public View(Context context, AttributeSet attrs, int defStyle);

     View就可以在构造函数中知道要去defStyle中的哪些属性出来
     属性集的引用不同于单个属性,使用R.styleable.name来引用

3)theme一般是针对activity的,全局生效,一般在theme中指定style,而这个指定,一般由一个属性来完成,属性在attr中定义

比如attr中定义autoCompletTextViewStyle属性,然后在theme中指定

<item name="autoCompleteTextViewStyle">@android:style/Widget.AutoCompleteTextView</item> 

这样就可以在View的构造函数中,利用R.attr.autoCompleteTextViewStyle取得Theme中指定的style

在 public View(Context context, AttributeSet attrs, int defStyle)中,defStyle既可以是某一个style:R.style.name,也可以是R.attr.name,对于后者,需要在theme中指定属性值。

转:http://blog.csdn.net/dlmu2001/article/details/7414411
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: