mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

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

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

改动页面布局背景: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 左下圆角半径

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

posted on 2017-05-31 18:59  mthoutai  阅读(275)  评论(0编辑  收藏  举报