7.Android学习之资源访问(一)

本文介绍了Android开发中资源访问的基础知识,包括字符串资源、颜色资源和尺寸资源的定义和使用。详细讲解了如何在资源文件中定义字符串、颜色和尺寸,并在Java和XML文件中引用这些资源。

目录

7.资源访问(一)

1.字符串资源(string)

1-1.定义字符串资源文件

1-2.使用字符串资源

2.颜色资源(color)

2-1.颜色值的定义

2-2.定义颜色资源文件

2-3.使用颜色资源

3.尺寸资源(dimen)

3-1.Android 支持的尺寸单位

3-2.使用尺寸资源


7.资源访问(一)

1.字符串资源(string)

在Android 中,当需要使用大量的字符串作为提示信息时,可以将这些字符串声明在资源文件中,从而实现程序的可配置性。

1-1.定义字符串资源文件

字符串资源文件位于res\values 目录下,在使用Android Sudio创建Android应用时,values目录下会自动创建字符串资源文件strings.xml (默认),该文件的基本结构如图7.1所示。

 

从图7.1中可以看出,在strings.xml文件中,根元素是<resources></resources>标记,在该元素中,使用<string></string>标记定义各字符串资源。其中,<string>标记的name属性用于指定字符串的名称,在<string>和</string>中间添加的内容为字符串资源的内容。

例如,在strings.xml资源文件中添加一个名称为“introduce”的字符串,内容是公司简介,具体代码如下:

<resources>
    <string name="app_name">demo</string>
    <string name="introduce">公司简介</string>
</resources>

此外,还可以创建新的字符串资源文件,具体方法如下:

(1)在values文件夹上单击鼠标右键,在弹出的快捷菜单中依次选择New→XML→Values XML file菜单项,将弹出创建资源文件的对话框,如图7.2所示。

 (2)在文本框中输入自定义的资源文件名称(例如string_user) ,单击Finish按钮,即可创建一个空的资源文件,代码结构如图7.3所示。

 

(3)在<resources> </resources>标记中使用<string></string>标记来创建字符串资源。

1-2.使用字符串资源

定义字符串资源后,就可以在Java文件或XML文件中使用字符串资源。

◆在Java文件中使用字符串资源的语法格式如下:

[<package>.]R.string.字符串名

例如,在MainActivity中要获取名称为“introduce”的字符串,可以使用下面的代码:

getResources().getString(R.string.introdue)

◆在XML文件中使用字符串资源的基本语法格式如下:

@[<package>:]string/字符串名

例如,在定义TextView组件时,通过字符串资源设置android:text属性值的代码如下:

< TextView
          android:layout_width="wrap_ content"
          android:layout_height="wrap_content"
          android:text="@string/introduce" />

2.颜色资源(color)

颜色资源也是Android应用开发常用的资源,用于设置文字和背景的颜色等。

2-1.颜色值的定义

在Android中,颜色值通过RGB(红、绿、蓝)色值和一个透明度(Alpha) 值表示。它必须以“#”开头,后面用Alpha-Red-Gren-Blue形式的内容。其中,Alpha 值可以省略,如果省略则表示颜色默认是完全不透明的。在通常情况下,颜色值使用如表7.1所示的4种形式之一。

表8.1 Android 支持的颜色值及其描述

颜色格式描述举例
#RGB使用红、绿、蓝三原色的值来表示颜色,其中,红、绿和蓝采用0~f来表示要表示红色,可以使用#f00
#ARGB使用透明度以及红、绿、蓝三原色来表示颜色,其中,透明度、红、 绿和蓝均采用0~f来表示要表示半透明的红色,可以使用#6f00
#RRGGBB使用红、绿、蓝三原色的值来表示颜色,与#RGB不同的是,这里的红、绿和蓝使用00~ff来表示要表示蓝色,可以使用#0000ff
#AARRGGBB使用透明度以及红、绿、蓝三原色来表示颜色,其中,透明度、红、绿和蓝均采用00~ff来表示要表示半透明的绿色,可以使用#6600ff00

注:在表示透明度时,0表示完全透明,f表示完全不透明。

2-2.定义颜色资源文件

颜色资源文件位于res\values目录下,在使用Android Studio创建Android项目时,values目录下会自动创建默认的颜色资源文件colors.xml,该文件的基本结构如图7.4所示。

从图中可以看出,在colors.xml文件中,根元素是<resources></resources>标记,在该元素中,使用<color></color>标记定义各颜色资源,其中,name属性用于指定颜色资源的名称,在<color>和</color>标记中间添加颜色值。

例如,在colors.xml资源文件中添加4个颜色资源,其中第1个名称为“title” ,颜色值采用#AARRGGBB格式;第2个名称为“title1”,颜色值采用#ARGB格式;第3个名称为“content" ,颜色值采用#RRGGBB格式;第4个名称为“content1” ,颜色值采用#RGB格式,关键代码如下:

<color name="title">#66ff0000</color>
<color name="title1">#6f00</color>
<color name="content">#ff0000</color>
<color name="content1">#f00</color>

注:第1个和第2个资源都表示半透明的红色;第3个和第4个资源都表示完全不透明的红色。

另外,还可以创建新的颜色资源文件,具体方法同1-1小节中介绍的创建新的字符串资源类似,只是设置的文件名不同。

在图7.4中可以看到每一行<color></color>标记的左侧都有一个小色块儿,如果不想使用该颜色,可以单击小色块,将打开选择颜色的对话框,如图7.5所示,在该对话框中选择想要使用的颜色。

 

2-3.使用颜色资源

定义颜色资源后,就可以在Java或XML文件中使用该颜色资源了。

◆在Java文件中使用颜色资源的语法格式如下:

[<package>.]R.color.颜色资源名

例如,在MainActivity中通过颜色资源为TextView组件设置文字颜色,可以使用下面的代码:

TextVieW tv=(TextView)findViewById(R.id.title);
tv.setTextcolor(getResources().getColor(R.color.title));

◆在XML文件中使用颜色资源的基本语法格式如下:

@[<package>:]color/颜色资源名

例如,在定义TextView组件时,通过颜色资源为其指定android:textColor属性,即设置组件内文字的颜色,代码如下:

<TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="@color/title" />

3.尺寸资源(dimen)

尺寸资源也是进行Android应用开发时比较常用的资源,它通常用于设置文字的大小、组件的间距等。

3-1.Android 支持的尺寸单位

Android支持的尺寸单位及其描述如表7.2所示。

表7.2 Android 支持的尺寸单位及其描述

尺寸单位描述适用于
dip或dp (设置独立像素)一种基于屏幕密度的抽象单位屏幕的清晰度
sp (比例像素)主要用于处理字体的大小,可以根据用户字体大小首选项进行缩放字体大小
px (Pixels, 像素)每个px对应屏幕上的一个点屏幕横向、纵向的像素个数
pt (points,磅)屏幕物理长度单位,1磅等于1/72 英寸设置字体大小(不常用)
in (Inches, 英寸)标准长度单位。1英寸等于2.54厘米屏幕对角线长度
mm (Millimeters, 毫米)屏幕物理长度单位屏幕物理长度

在表7.2列出的几种尺寸单位中,比较常用的是dp和sp。

◆dp

在屏幕密度为160dpi (每英寸160点)的显示器上,1dp=1px。 随着屏幕密度的改变,dp 与px的换算也会发生改变。例如,在屏幕密度为320dpi的显示器上,1dp= 2px。

◆sp

与dp类似,该尺寸单位主要用于字体显示,它可以根据用户对字体大小的首选项进行缩放。因此,字体大小使用sp单位设置可以确保文字按照用户选择的大小显示。

3-2.使用尺寸资源

尺寸资源文件位于res\values目录下,在使用当前版本的Android Studio 创建Android项目时,不会自动创建尺寸资源文件,需要在values目录下手动创建尺寸资源文件dimen.xml,在values节点上单击鼠标右键,在弹出的菜单中依次选择New→Values resource file选项,如图7.6所示。

 

将弹出New Resource File对话框,在该对话框中填写资源文件名称,然后单击OK按钮,即可创建尺寸资源文件dimens.xml,如图7.7所示。

 

该文件默认的基本结构如图7.8所示。

 

在dimens.xml文件中,根元素是<resources></resources>标记,在该元素中,使用<dimen></dimen>标记定义各尺寸资源,其中,name属性用于指定尺寸资源的名称,在<dimen>和</dimen>标记中间定义一个尺寸常量。

例如,在dimens.xml资源文件中添加两个尺寸资源,其中一个名称为“title",尺寸值为24sp;另一个名称为“content",尺寸值为14dp,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="title">24sp</dimen>
    <dimen name="content">14dp</dimen>
</resources>

定义尺寸资源后,就可以在Java或XML文件中使用该尺寸资源了。

◆在Java文件中使用尺寸资源的语法格式如下:

[<package>.]R.dimen.尺寸资源名

例如,在MainActivity 中,通过尺寸资源为TextView组件设置文字大小,可以使用下面的代码:

TextView tv=(TextView)findViewById(R.id.title);
tv.setTextSize(getResources().getDimension(R.dimen.title));

◆在XML文件中使用尺寸资源的基本语法格式如下:

@[<package>:]dimen/尺寸资源名

例如,在定义TextView组件时,通过尺寸资源为其指定android:textSize属性,即设置组件内文字的大小,代码如下:

 <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textsize="@dimen/title" />

例:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="120dp"
    android:paddingBottom="120dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical"
    android:background="@drawable/bg"
    tools:context=".MainActivity">
​
<!--    第一行-->
    <LinearLayout
        android:id="@+id/main_ll1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
​
        <TextView
            android:id="@+id/main_tv1"
            android:text="@string/tv1"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv1"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
​
        <TextView
            android:id="@+id/main_tv2"
            android:text="@string/tv2"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv2"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"/>
​
        <TextView
            android:id="@+id/main_tv3"
            android:text="@string/tv3"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv3"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"/>
    </LinearLayout>
​
<!--    第二行-->
    <LinearLayout
        android:id="@+id/main_ll2"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginTop="@dimen/margin">
​
        <TextView
            android:id="@+id/main_tv4"
            android:text="@string/tv4"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv4"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
​
        <TextView
            android:id="@+id/main_tv5"
            android:text="@string/tv5"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv5"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"/>
​
        <TextView
            android:id="@+id/main_tv6"
            android:text="@string/tv6"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv6"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"/>
    </LinearLayout>
​
<!--    第三行-->
    <LinearLayout
        android:id="@+id/main_ll3"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginTop="@dimen/margin">
​
        <TextView
            android:id="@+id/main_tv7"
            android:text="@string/tv7"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv7"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
​
        <TextView
            android:id="@+id/main_tv8"
            android:text="@string/tv8"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv8"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"/>
​
        <TextView
            android:id="@+id/main_tv9"
            android:text="@string/tv9"
            android:textSize="@dimen/wordsize"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@color/tv9"
            android:textColor="@color/wordcolor"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"/>
    </LinearLayout>
​
</LinearLayout>

res\values\dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--    文字的大小-->
    <dimen name="wordsize">18sp</dimen>
<!--    文本框的外边距-->
    <dimen name="margin">5dp</dimen>
</resources>

res\values\colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="wordcolor">#FFFFFF</color>
    <color name="tv1">#BBE24A83</color>
    <color name="tv2">#BB318AD6</color>
    <color name="tv3">#BBD73943</color>
    <color name="tv4">#BBE69A08</color>
    <color name="tv5">#BBBD9663</color>
    <color name="tv6">#BBD45ABC</color>
    <color name="tv7">#BB4AA6D6</color>
    <color name="tv8">#BB8064D2</color>
    <color name="tv9">#BBF7A81E</color>
</resources>

res\values\strings

<resources>
    <string name="app_name">Windows Phone</string>
    <string name="tv1">微信</string>
    <string name="tv2">通讯录</string>
    <string name="tv3">QQ</string>
    <string name="tv4">相机</string>
    <string name="tv5">时钟</string>
    <string name="tv6">备忘录</string>
    <string name="tv7">音乐</string>
    <string name="tv8">互联网</string>
    <string name="tv9">邮件</string>
</resources>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值