Android:认识R类、findViewById方法查找组件、@string查找字符、@color查找颜色、@drawable查找图片、@dimen某个组件尺寸定义、项目引入资源国际化
导入
之前都是断断续续的看了一些于如何使用android开发的文章、资料等,到目前位置很多基础的东西都不清楚,于是去学习了别人的课程,才了认识了R类、findViewById方法查找组件、项目引入资源国际化、@string查找字符。
在学习之前我们需要安装环境,这里我采用开发环境是:win7 x64+jdk8+adt-bundle-windows-x86_64-20131030.zip(adt-bundle是集成了adt、eclipse、andrlid sdk等,解压运行eclipse.exe就可以直接使用)
jdk8+adt-bundle-windows-x86_64-20131030.zip下载链接: https://pan.baidu.com/s/1gfoyKN5 密码: d8dt
adt-bundleide运行效果如下:

认识R类
R类管理资源包含:
1)layout下中的andoid:id、android:text等资源信息等
public static final class id {
public static final int action_settings=0x7f080002;
public static final int button1=0x7f080001;
public static final int textView1=0x7f080000;
}
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050002;
public static final int main_activity_button_title=0x7f050005;
public static final int main_activity_msg=0x7f050003;
public static final int main_activity_textView_text=0x7f050004;
}
2)string对应的字段是res/values/strings.xml中的配置项信息(自动生成的,不需要认为的修改R类,包含id也一样)。
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">HelloWord</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="main_activity_msg">Welcome hello word!</string>
<string name="main_activity_textView_text">text view text,hello word!</string>
<string name="main_activity_button_title">Button</string> </resources>
3)@color查找颜色
color对应的字段是res/values/colors.xml中的配置项信息(自动生成的,不需要认为的修改R类,包含id也一样)。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#FFFF00</string>
<color name="red">#FF0000</string>
<color name="black">#000000</string>
</resources>
使用时:
<Button
...
android:background="@color/red"/>
4)@drawable查找图片
只需要把png/jpeg/gif文件拷贝到新建的/res/drawable目录下,或者拷贝到工程新建的默认drawable-xx目录下
使用时:
<xx
android:src="@drawable/img1"
/>
注意:官网推荐使用png,不推荐使用gif图片。
drawable-xx文件夹的区别:

5)@dimen某个组件尺寸定义
需要在res/values/目录下新建一个dimen.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="btn_height">24dp</dimen>
<dimen name="btn_weight">56dp</dimen>
</resources>
尺寸单位:

findViewById方法查找组件
为了实现findViewById查找组件,于是在新建的android项目的MainActivity布局中拖拉如一个buttong(id=button1)
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="47dp"
android:layout_marginTop="72dp"
android:onClick="test"
android:text="Button" />
button1添加android:onClick事件,并在MainActivity.java中添加监听函数test
public void test(View view) {
Toast.makeText(MainActivity.this, R.string.main_activity_msg,
Toast.LENGTH_SHORT).show();
TextView textView = (TextView) this.findViewById(R.id.textView1);
textView.setText(R.string.main_activity_textView_text);
}
备注:
1)其中test事件函数的参数:响应的是哪个控件则参数view就是哪个控件对象;
2)Toast.makText是弹出提示框用的,想javascript中的alter函数;
3)findViewById这里是通过R.id.textView1来搜索textView1,找到后修改了textView1的text属性值。
@string查找字符
在我们默认添加一个button等按钮时,其text值默认是Button值。这样是android不推荐的使用方式,不利于国际化配置,扩展性不好。
上边我们的textView.text修改时采用了R.string.man_activity_textView_text的找到了新的text,该text使其是配置在res/values/strings.xml配置项中,其实我们这里的button也是支持从strings.xml配置项读取方式:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="47dp"
android:layout_marginTop="72dp"
android:onClick="test"
android:text="@string/main_activity_button_title" />
项目引入资源国际化
步骤一:在res/下新建中文资源国际化文件夹values-zh-rCN;
步骤二:把res/values下的strings.xml拷贝到res/values-zh-rCN下,并修改res/values-zh-rCN/strings.xml内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloWord</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="main_activity_msg">欢迎进入!</string>
<string name="main_activity_textView_text">textView的内容!</string>
<string name="main_activity_button_title">按钮</string>
</resources>
重新发布项目,并修改测试机的语言设置为中文,之后查看该app:

修改测试机的语言设置为英文,之后查看该app:

Android:认识R类、findViewById方法查找组件、@string查找字符、@color查找颜色、@drawable查找图片、@dimen某个组件尺寸定义、项目引入资源国际化的更多相关文章
- Android之TelephonyManager类的方法详解
TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法.其中包括手机SIM的状态和信息.电信网络的状态及手机用户的信息.在应用程序中可以使用这些get方法获取 ...
- Android中GPS类及方法简介
GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供定位功能.GPS定位是一门高新技术,但对于Android程序员来说,开发GPS功能的应用程 ...
- android Activity基类通用方法
public class BaseActivity extends Activity { Resources res; // 通用资源缩写 @Override protected void onCre ...
- 为什么android的R类要定义成16进制
联想到c语言中的宏定义:我想是一个原因 如: #define SDL_INIT_TIMER 0x00000001 #define SDL_INIT_AUDIO 0x00000010 #define S ...
- java 调用 C# 类库搞定,三步即可,可以调用任何类及方法,很简单,非常爽啊
java 调用 C# 类库搞定,三步即可,可以调用任何类及方法,很简单,非常爽啊 java 调用 C# 类库搞定,可以调用任何类及方法,很简单,非常爽啊 总体分三步走: 一.准备一个 C# 类库 (d ...
- 安卓中Paint类和Canvas类的方法汇总
Paint类的常用的方法 1.setColor方法,用于设置画笔的颜色,public void setColor(int color)//参数color为颜色值,也可以使用Color类定义的颜色Col ...
- 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod
定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...
- 关于Android项目中,突然就R类找不到已存在的资源文件的解决方法
项目代码早上打开正常,下午开的时候突然提示R类找不到已存在的布局文件,于是试了各种方法,CLEAN啊,重启啊,均无效,然后去网上搜了下,遇到这个问题的人还不少. 看到其中有这么一条解决方法,删除导入的 ...
- Android笔记:如何在Fragment里使用findViewById()方法?
在Activity中,可以直接用findViewById(int id),通过xml的id来找到对应的View. 查找官方的api,具体如下: https://developer.android.go ...
随机推荐
- 使用kafka connect,将数据批量写到hdfs完整过程
版权声明:本文为博主原创文章,未经博主允许不得转载 本文是基于hadoop 2.7.1,以及kafka 0.11.0.0.kafka-connect是以单节点模式运行,即standalone. 首先, ...
- sklearn包中有哪些数据集你都知道吗?
注册了博客园一晃有3个月了,同时接触机器学习也断断续续的算是有1个月了.今天就用机器学习神器sklearn包的相关内容作为我的开篇文章吧. 本文将对sklearn包中的数据集做一个系统介绍,并简单说一 ...
- ASCII代码
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧 ...
- 最小化安装CentOS7的网卡设置
实验环境:CentOS 7 Minimal Installation 64bit (1511) 最小化安装CentOS 7 后,查看网卡的信息让人很意外,因为网卡的命名规则变了,网卡的名字让人很难懂. ...
- MIP (百度移动网页加速器)
前言:第一次用移动网页加速器,感觉好心情都被弄坏了.确实性能提高了不少,但是限制js,对于一些交互实现都成问题.MIP是Mobile Instant Pages的缩写,指百度移动网页加速器, 是一套应 ...
- docker教程
Docker 包括三个基本概念 镜像(Image) 容器(Container) 仏库(Repository) 理解了返三个概念,就理解了 Docker 的整个生命周期. Docker 镜像 Docke ...
- Redis --> Ubuntu安装redis
Ubuntu安装redis 一.下载安装 root@21ebdf03a086:/# apt-cache search redis root@21ebdf03a086:/# apt-get inst ...
- 将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hibernate)整合配置
配置web.xml,applicationContext.xml, spring-mvc.xml,applicationContext-shiro.xml,而且都有详细的说明. Web.xml是web ...
- 四则运算题目生成(python版)
四则运算题目生成-基于控制台 项目托管在码云:飞机票 需求分析 根据控制台提示信息,输入题目生成相关配置参数 题目生成数量 数字范围 式子中是否有分数 .... 程序支持 10000 题目生成 题目与 ...
- 根据IO流源码深入理解装饰设计模式使用
一:摘要 通过对java的IO类中我们可以得出:IO源码中使用装饰设计模式频率非常高, 对装饰设计模式而言,他能够避免继承体系的臃肿,同时也可以动态的给一个对象添加一些额外的功能,如果要扩展一个功能, ...