android中各种资源的使用:

在android开发中,各种资源的合理使用应该在各自的xml中进行定义,以便反复使用;

字符串资源:strings.xml,xml中引用:@string/XXX,java代码中引用:R.string.XXX

样式资源:styles.xml,xml中引用:@style/XXX,java代码中引用:R.style.XXX

图片资源:colors.xml,xml中引用:@color/XXX,java代码中引用:R.color.XXX

尺寸资源:dimens.xml。xml中引用:@dimen/XXX,java代码中引用:R.dimen.XXX

.............

依据各资源定义,实现的简单的效果图 :

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGFyZGVyWGlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

改动页面布局背景:android:background="#f0f0e0",也能够引用color.xml中定义的资源

1、在values目录中新建colors.xml文件:

<?

xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tv_text_color">#FF33CC</color>
<color name="btn_text_color">#0033CC</color>
</resources>

上面中定义了TextView和Button中字体颜色,页面使用:@color/tv_text_color

   <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="測试字体样式"
android:textColor="@color/tv_text_color" />

2、在values中新建styles.xml文件:定义了统一的style样式,能够在应用程序中多处使用

<?xml version="1.0" encoding="utf-8"?

>
<resources>
<style name="AppTheme" parent="android:Theme.Light"></style> <style name="content_style">
<item name="android:minHeight">50dp</item>
<item name="android:gravity">left|center</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">15sp</item>
<item name="android:lineSpacingExtra">2dp</item>
<item name="android:background">#bfbfbf</item>
</style>
</resources>

在xml中引用:给TextView定义style样式:@style/content_style

    <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="測试style样式"
style="@style/content_style"/>

3、给Button加入背景图片:android:background="@drawable/btn_bg1",当中btn_bg1为放在drawable-hdpi、ldpi、mdpi中的图片资源,hdpi、ldpi、mdpi分别代表高分辨率、低分辨率、中分辨率,是依据android手机的屏幕分辨率来决定的;

4、有时候。我们有这种需求,button初始化为黄绿色,当我们点击时候变成红色背景。类似以下的需求:

初始化:

点击后:

这时候,我们能够在xml中借助选择器来定义,selector,我们在res下新建目录drawable,然后编写我们的selector对应的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_bg2" />
<item android:state_focused="true" android:drawable="@drawable/btn_bg4"/>
<item android:drawable="@drawable/btn_bg1" />
</selector>

上面定义中:

button按下锁显示的图片:

<item android:state_pressed="true" android:drawable="@drawable/btn_bg2" />

聚焦所显示的图片:

<item android:state_focused="true" android:drawable="@drawable/btn_bg4"/>

默认显示的图片:

<item android:drawable="@drawable/btn_bg1" />

里面还有非常多的属性供我们选择!

我们在xml中引用它:android:background:@drawable/对应的selector文件名称.xml

java代码:R.drawable.对应的selector文件名称.xml

5、定义带圆角的矩形button:相同是依据selector来选择,仅仅只是它里面也能够定义多种shape属性,shape表示定义多种形状。用的也非常多。能够给我们的界面带来非常丰富的体验:

<?

xml version="1.0" encoding="utf-8"?

>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<gradient android:angle="270" android:startColor="#A5D245"
android:endColor="#99BD4C"/>
<size android:height="60dp" android:width="320dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
<item android:state_pressed="true">
<shape>
<gradient android:angle="270" android:startColor="#A5D245"
android:endColor="#99BD4C"/>
<size android:height="60dp" android:width="320dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
<item>
<shape>
<gradient android:angle="270" android:startColor="#A8C3B0"
android:endColor="#C6CFCE"/>
<size android:height="60dp" android:width="320dp"/>
<corners android:radius="8dp"/>
</shape>
</item>
</selector>

shape中属性定义解释:

<shape>  android:shape=["rectangle" | "oval" | "line" | "ring"]

当中rectagle矩形,oval椭圆。line水平直线,ring环形

<shape>中子节点的经常使用属性:

<gradient>  渐变

android:startColor  起始颜色

android:endColor  结束颜色

android:angle  渐变角度,0从上到下。90表示从左到右。数值为45的整数倍默觉得0。

android:type  渐变的样式 liner线性渐变 radial环形渐变 sweep

<solid >  填充

android:color  填充的颜色

<stroke > 描边

android:width 描边的宽度

android:color 描边的颜色

android:dashWidth 表示'-'横线的宽度

android:dashGap 表示'-'横线之间的距离

<corners > 圆角

android:radius  圆角的半径 值越大角越圆

android:topRightRadius  右上圆角半径

 

android:bottomLeftRadius 右下圆角角半径

 

android:topLeftRadius 左上圆角半径

android:bottomRightRadius 左下圆角半径

项目中我们还有非常多资源须要我们去自己查找学习和灵活运用!!

Android之旅十六 android中各种资源的使用的更多相关文章

  1. Android之旅十四 android中的xml文件解析

    在我们做有关android项目的时候,肯定会涉及到对xml文件的解析操作.以下给大家介绍一下xml文件的解析.包括DOM.SAX.Pull以及曾经我们用到的DOM4J和JDOM: 要解析的XML文件: ...

  2. J2EE进阶(十六)Hibernate 中getHibernateTemplate()方法使用

    J2EE进阶(十六)Hibernate 中getHibernateTemplate()方法使用   spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修 ...

  3. Android中GridView拖拽的效果【android进化三十六】

      最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的 ...

  4. Android笔记(六十六) android中的动画——XML文件定义属性动画

    除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...

  5. Android之旅十八 百度地图环境搭建

    在android中使用百度地图,我们能够先看看百度地图对应的SDK信息:http://developer.baidu.com/map/index.php? title=androidsdk,它里面基本 ...

  6. Android学习之基础知识十六 — Android开发高级技巧的掌握

    一.全局获取Context的技巧 前面我们很多地方都使用到了Context,弹出Toast的时候.启动活动的时候.发送广播的时候.操作数据库的时候.使用通知的时候等等.或许目前来说我们并没有为得不到C ...

  7. android 学习随笔十六(广播 )

    1.广播接收者 BroadcastReceiver 接收系统发出的广播 现实中的广播:电台为了传达一些消息,而发送的广播,通过广播携带要传达的消息,群众只要买一个收音机,就可以收到广播了  Andro ...

  8. Android入门第十六篇之Style与Theme [转]

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 越来越多互联网企业都在Android平台上部署其客户端,为了提升用户体验,这些客户端都做得布局合理而且美观.. ...

  9. Android基调(十六)- Service:startService()、stopService()、bindService()、unbindService()加

    直行 第一 另外一种 第三种 总结 开门见山 开启服务有三种情况:假设直接使用服务,则没有必要进行绑定,可是假设要使用服务里面的方法.则要进行绑定. 具体的启动情况有下: ①调用startServic ...

随机推荐

  1. objc_setAssociatedObject获取cell上button对应所在的行

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...

  2. python--6、logging模块

    logging 可用的日志级别: debug 10 info 20 warning 30 error 40 critical 50 logging默认参数: 默认日志级别是warning. 默认情况日 ...

  3. 2B课程笔记分享_StudyJams_2017

    课程2B-创建交互式应用(下) 概述 课程2B的内容主要包括:使用变量来更新欲显示在屏幕上的内容,为按钮添加事件响应(联系XML属性与Java方法)逻辑等. 后续的课程会逐步深入地讲解使用Java开发 ...

  4. (转) Arcgis for js加载百度地图

    http://blog.csdn.net/gisshixisheng/article/details/44853709 概述: 在前面的文章里提到了Arcgis for js加载天地图,在本节,继续讲 ...

  5. How to add jdk8 in Eclipse Indigo

    I just read How to have Eclipse use JDK8 to compile a project? What i added jdk8 to eclipse as,  Fro ...

  6. js document 触发按键事件

    // 键盘控制 var keyEvent = (function () { document.onkeydown = function (e) { if (e.keyCode === 38) { // ...

  7. js获取当前具体时间

    /** * 获取当前时间 * @param isTime true:显示日期和时间,如:2018-09-20 13:25:12:false:显示日期,如:2018-09-20. * @returns ...

  8. jsTree checkbox plugin使用笔记

    引入css文件 <link rel="stylesheet" type="text/css" href="js/assets/global/pl ...

  9. 如何查看Linux的CPU负载

    哪些工具可以查看 CPU 负载? 可以使用 top 命令.uptime 命令,特别是 top 命令,功能强大,不仅仅可以用来查看 CPU 负载. CPU 负载怎么理解?是不是 CPU 利用率? 要区别 ...

  10. ffmpeg中关于EAGAIN的理解及非阻塞IO

    ffmpeg为在linux下开发的开源音视频框架,所以经常会碰到很多错误(设置errno),其中EAGAIN是其中比较常见的一个错误(比如用在非阻塞操作中).  try again,从字面上来看,是提 ...