android中?attr/**与@drawable/**或@color/**等的区别
来源:互联网 发布:江铠同翟天临分手知乎 编辑:程序博客网 时间:2024/04/19 06:08
今天在写一个Demo的时候,突然遇到?attr/colorPrimary这个用法,一百度,竟然全网都是同一篇文章,
我苦思冥想,最终还搬出了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的一个小总结。。。。
1 0
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别
- android中?attr/**与@drawable/**或@color/**等的区别及用法
- Android中 @和?区别以及?attr/**与@style/**等的区别
- Android中 @和?区别以及?attr/**与@style/**等的区别
- color.xml 中定义drawable和color的区别
- android中drawable 与mipmap的区别
- 选择器selector中drawable和color的区别
- jquery中attr与prop的区别
- jQuery中attr()与prop()的区别
- jquery中attr()与prop()的区别
- jQuery中attr()与prop()的区别
- 论Android Studio 中 mipmaps 与 drawable 的区别?
- Android中 mipmap与drawable区别
- 获取Android Res文件下的Drawable,Color等文件出现的异常总结
- 第一篇
- 小娜老师的讲义-Docker基础知识-网络基础配置
- 【每天进步一点点】jquery 自动补全autocomplete
- MyEclipse中使用Maven创建web项目
- MFC通过URL下载并保存文件代码
- android中?attr/**与@drawable/**或@color/**等的区别
- JsonView beta1
- Android逆向工程之反编译注入代码
- Java NIO学习笔记之二-图解ByteBuffer
- 项目中有多个resource文件夹时,load问题
- Tomcat的server.xml配置文件解析
- 几个字符串函数的实现
- String类型内存转换,你明白为什么吗?
- android studio找不到subversion