您现在的位置是:首页 > 文章详情

2-VIV-Android控件之res资源加载

日期:2018-09-02点击:382

零、前言

[1].本想在控件使用中穿插讲一下资源在加载,但感觉知识点挺多还是单开一篇专门讲一下吧。
[2].values/string.xml的使用
[3].values/dimens.xml的使用
[4].values/colors.xml的使用
[5].values/styles.xml的使用
[6].选择器selector的使用
[7].anim里xml动画加载


一、values/string.xml的使用

1.单字符串
<string name="title">张风捷特烈</string> 
//使用单字符串 setTitle(getResources().getString(R.string.title)); //xml里 android:text="@string/title" 
定义string.png
2.字符串数组
<string-array name="coder"> <item>Java</item> <item>Python</item> <item>JavaScript</item> <item>C++</item> <item>C</item> </string-array> 
//获取字符数组 String[] coders = getResources().getStringArray(R.array.coder); for (int i = 0; i < coders.length; i++) { mIdTvCoder.append(coders[i]+"\n"); } 
加载数组.png

二、values/dimens.xml的使用

<dimen name="sp_18">18sp</dimen> 
//xml android:textSize="@dimen/sp_18" //获取尺寸==已转换为px float dimension = getResources().getDimensionPixelOffset(R.dimen.sp_18); mIdTvCoder.setTextSize(TypedValue.COMPLEX_UNIT_PX,dimension); 

三、values/styles.xml的使用

用来抽取一些常用样式

<style name="SmallBlueTextView"> <item name="android:layout_height">wrap_content</item> <item name="android:layout_width">wrap_content</item> <item name="android:textSize">@dimen/sp_12</item> <item name="android:textColor">@dimen/sp_12</item> </style> 

使用

 <TextView android:id="@+id/id_tv_coder" style="@style/SmallBlueTextView"/> 

四、values/colors.xml的使用

<color name="gray">#eeeeee</color> 
//获取颜色 int color = getResources().getColor(R.color.gray); mIdTvCoder.setBackgroundColor(color); //xml里 android:background="@color/gray" 

颜色.png

五、选择器selector的使用

1.drawable文件夹下的selector:drawable/sel_db_gary_white.xml

颜色部分都可以改成相应的图片资源,这里就不赘述了

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--按压为灰色--> <item android:drawable="@color/gray" android:state_pressed="true"/> <!--不可用状态为红色--> <item android:drawable="@color/red" android:state_enabled="false"/> <!--默认白色--> <item android:drawable="@color/white"/> </selector> 

当做一个drawable使用

 android:background="@drawable/sel_db_gary_white" 

常见属性

 1、android:drawable 可绘制对象资源。 2、android:state_pressed 按下 3、android:state_checked 选中 4、android:state_checkable 可选中 5、android:state_selected 方向键浏览列表 6、android:state_enabled 可用 7、android:state_focused 获取焦点 8、android:state_activated 激活 9、android:state_hovered 光标悬停 10、android:state_window_focused 窗口有焦点 
2.color文件夹下的selector:color/sel_col_gary_white.xml

注意小坑,只能给文字用,背景一用就崩了!!!
在res文件夹下新建color文件夹

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#A6A6A6" android:state_enabled="false"/> <item android:color="#A6D3CF" android:state_pressed="true"/> <item android:color="@color/blue"/> </selector> 

xml中当作颜色使用

android:textColor="@color/sel_col_gary_white" 

代码中使用

//获取选择器 ColorStateList selColor = getResources().getColorStateList(R.color.sel_col_gary_white); mIdTvCoder.setTextColor(selColor); 

六、anim里xml动画加载

定义

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatMode="reverse" android:toDegrees="360" /> </set> 

使用

Animation rotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate); mIdTvCoder.setAnimation(rotate); mIdTvCoder.startAnimation(rotate); 

后记、

1.声明:

[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力

2.连接传送门:

更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

3.联系我

QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328

4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
公众号.jpg
原文链接:https://yq.aliyun.com/articles/633810
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章