Android 中style attr declare-styleable theme以及引用方式

来源:互联网 发布:在线ps网站源码 编辑:程序博客网 时间:2024/05/29 00:30

大家知道一个控件在xml中定义可以设置属性,那知不知道设置属性有那些方式以及这些方式的优先级,是否知道属性的引用方式:

<1>控件的属性的设置的五种方式:

  1. 直接在xml中直接定义
  2. 通过Style来引用外部样式
  3. 在对应的主题中定义外部Style引用(也就是控件的第三个参数defStyleAttr如:textViewStyle,buttonStyle,preferenceStyle等等)
  4. 在对应的默认style定义(也就是控件的第四个参数defStyleRes,一般都为0,也就是没有)
  5. 在对应的主题直接定义
    以上的优先级基本是从上到下,4的优先级有些特殊,当3中defStyleAttr不为0且主题中定义了此属性不会生效。
    ###<2>控件的属性实现方式
    直接看TextView 源码:
public TextView(            Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {            ……        super(context, attrs, defStyleAttr, defStyleRes);        TypedArray a = theme.obtainStyledAttributes(attrs,                com.android.internal.R.styleable.TextViewAppearance, defStyleAttr, defStyleRes);textColor = appearance.getColorStateList(com.android.internal.R.styleable.TextAppearance_textColor);……

attrs中是xml和style属性的集合(可能有些属性不是本控件的)
defStyleAttr 是对应主题中的外部样式引用
defStyleRes是默认样式
com.android.internal.R.styleable.TextViewAppearance是textview中的属性申明集合,此集合通过declare-styleable XML标签定义(也可以直接定义attr ,再用int数组引用)com.android.internal.R.styleable.TextViewAppearance在R 类中styleable类的一个int数组

com.android.internal.R.styleable.TextAppearance_textColor是textview中的属性申明集合中的textcolor属性下标,也就是在R 类中styleable类的一个int数组的下标

<3>主题的定义和优先级

  1. 在代码中设置或者创建是指定:
    如Activity的setTheme方法,Dialog(context, themeResId)在构造函数中指定
    2.在Activity的配置xml配置
    3.在Appication的配置xml配置
    优先级从上到下

<4>android中资源引用使用

1.@代表引用资源
引用自定义资源。格式:@[package:]type/name
引用app中资源 android:text=”@string/hello”
引用系统中资源
android:textColor=”@android:color/opaque_red”
android:id=”@android:id/text1”
2.@*代表引用系统的非public资源。格式:@*android:type/name
系统资源定义分public和非public。public的声明在:

<sdk_path>\platforms\android-8\data\res\values\public.xml

@*android:type/name:可以调用系统定义的所有资源
@android:type/name:只能够调用publi属性的资源。
注意:没在public.xml中声明的资源是google不推荐使用的。
3.?代表引用主题属性
?[namespace:]type/name,这里类型可选
android:textColor=”?android:textDisabledColor”

 <item name="wifi_signal_color">?android:attr/colorAccent</item>

4.@+代表在创建或引用资源 。格式:@+type/name
”+”表示在R.java中名为type的内部类中添加一条记录
android:id=”@+id/selectdlg” 新建一个资源ID

还需要注意在主题中引用的外部style,在此style引用的属性只能引用对应Theme的属性
如下代码:

Activity 主题 <style name="Theme.Settings" parent="Theme.SettingsBase">  <item name="android:colorAccent">#ff6900</item>  <item name="android:alertDialogTheme">@style/Theme.AlertDialog</item> </style>dialog 主题<style name="Theme.AlertDialog" parent="@*android:style/Theme.Material.Settings.Dialog.Alert">       name="android:buttonBarButtonStyle">@style/CustomButtonStyle</item>        <item name="android:colorAccent">#ff6900</item>    </style>AlertDialog下面三个按钮样式    <style name="CustomButtonStyle" parent="@*android:style/Widget.Material.Button">        <!-- 添加Button style -->        <item name="android:textColor">?android:attr/colorAccent</item>        <item name="android:background">@null</item>    </style>

如果Theme.AlertDialog不添加

 <item name="android:colorAccent">#ff6900</item>

是无法应到Activity 主题的android:colorAccent的

1 0
原创粉丝点击