android中?attr/**与@drawable/**或@color/**等的区别
2017-02-24 20:08
344 查看
今天在写一个Demo的时候,突然遇到?attr/colorPrimary这个用法,一百度,竟然全网都是同一篇文章,
我苦思冥想,最终还搬出了Android源码,终于知道了意思,暂且记下来,作为备忘录吧。
一、?attr/**
这个google叫预定义样式
这个是用在多主题时的场景,属性值会随着主题而改变。
但并不是一上来就可以用的,必须做足以下准备工作:
1,如果是自定义控件,请在style.xml中或attrs.xml中声明属性:
如红色字体所示,必须指明format为reference
2,因为attr/是跟随Theme来变化的,所以对attr跟随的属性必须在Theme里面声明:
3,在对应的属性color,drawable等里面加入相应的资源
4,这样我就可以在xml中使用自定义控件的自定义属性,来随着主题而改变:
二、@color,@drawable等
这个就是我们平时最常用的,就是指定资源,不是动态的,不会随着主题变化
以上,就是?attr/与@color,@drawable的一个小总结。
?attr中的问号表示这是一个资源引用,并且?attr表示引用的是当前主题中的资源。而?android:attr/表示引用的是android系统中的一些资源。
我苦思冥想,最终还搬出了Android源码,终于知道了意思,暂且记下来,作为备忘录吧。
一、?attr/**
这个google叫预定义样式
这个是用在多主题时的场景,属性值会随着主题而改变。
但并不是一上来就可以用的,必须做足以下准备工作:
1,如果是自定义控件,请在style.xml中或attrs.xml中声明属性:
<declare-styleable name="SunnyAttr"> <attr name="sunnyTextColor" format="reference"/> <attr name="sunnyBgColor" format="reference"/> <attr name="sunnyTextColorWhite" format="color"/> <attr name="sunnyTextColorRed" format="reference"/> <attr name="textColor" format="reference"></attr> </declare-styleable>
如红色字体所示,必须指明format为reference
2,因为attr/是跟随Theme来变化的,所以对attr跟随的属性必须在Theme里面声明:
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="sunnyTextColorRed">@color/sunnyTextColorYellow</item> </style>
3,在对应的属性color,drawable等里面加入相应的资源
<color name="sunnyTextColorRed">#FFFF0000</color>
4,这样我就可以在xml中使用自定义控件的自定义属性,来随着主题而改变:
<com.smartbracelet.sunny.sunnydemo3.SunnyTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="设置界面" app:sunnyTextColor="?attr/sunnyTextColorRed" />
二、@color,@drawable等
这个就是我们平时最常用的,就是指定资源,不是动态的,不会随着主题变化
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="红色" android:textColor="@color/sunnyTextColorRed" />
以上,就是?attr/与@color,@drawable的一个小总结。
?attr中的问号表示这是一个资源引用,并且?attr表示引用的是当前主题中的资源。而?android:attr/表示引用的是android系统中的一些资源。
相关文章推荐
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别及用法
- Android Drawable、Bitmap、Canvas和Paint的区别
- Android Drawable、Bitmap、Canvas和Paint的区别 .
- 浅析Android中Drawable(hdpi,ldpi,mdpi)目录的区别
- Android中Drawable Bitmap Canvas Paint之间区别
- Android开发中的drawable-(hdpi,mdpi,ldpi)和WVGA,HVGA,QVGA的区别以及联系
- Android开发中的drawable-(hdpi,mdpi,ldpi)和WVGA,HVGA,QVGA的区别以及联系
- [Android]代码实现ColorStateList及StateListDrawable
- Android2.1中的 drawable(hdpi,ldpi,mdpi) 的区别
- Android2.1中的 drawable(hdpi,ldpi,mdpi) 的区别
- android drawable-(hdpi,mdpi,ldpi)的区别和关于surfaceholder.lockCanvas(rect)方法的疑问
- android 中Drawable、Bitmap、Canvas和Paint的区别
- Android Drawable、Bitmap、Canvas和Paint的区别
- Android开发中的drawable-(hdpi,mdpi,ldpi)和WVGA,HVGA,QVGA的区别以及联系
- android drawable-hdpi drawable-mdpi draw-ldpi 区别
- Android开发中的drawable-(hdpi,mdpi,ldpi)和WVGA,HVGA,QVGA的区别以及联系 还有 什么是XGA,WXGA,WUXGA,和WSXGA+?