资源目录
- 有许多东西用来构建一个优秀的 Android 应用程序,除了应用程序的编码,还需要关注各种各样的资源,诸如用到的各种静态内容,位图,颜色,布局定义,用户界面字符串,动画等等。这些资源一般放置在项目的 res/ 下独立子目录中。
- res/ 目录在各种子目录中包含了所有的资源。
目录 | 资源类型 |
---|---|
drawable/ | 图片文件,如.png,.jpg,.gif 或者 XML文件,被编译为位图、状态列表、形状、动画图片。它们被保存在 res/drawable/文件夹下,通过 R.drawable 类访问 |
layout/ | 定义用户界面布局的 XML 文件。它们被保存在 res/layout/ 文件夹下,通过 R.layout 类访问 |
menu/ | 定义应用程序菜单的 XML 文件,如选项菜单,上下文菜单,子菜单等。它们被保存在 res/menu/ 文件夹下,通过 R.menu类访问 |
mipmap | 应用程序的图标 |
raw/ | 任意的文件以它们的原始形式保存。需要根据名为 R.raw.filename 的资源ID,通过调用 Resource.openRawResource() 来打开 raw 文件 |
values/ | 包含简单值(如字符串,整数,颜色等)的 XML 文件。这里有一些文件夹下的资源命名规范。1)arrays.xml 代表数组资源,通过R.array类访问;2)integers.xml 代表整数资源,通过R.integer类访问;3)bools.xml代表布尔值资源,通过R.bool类访问;4)colors.xml代表颜色资源,通过R.color类访问;5)dimens.xml代表维度值,通过R.dimen类访问;6)strings.xml代表字符串资源,通过R.string类访问;7)styles.xml代表样式资源,通过R.style类访问 |
访问资源
- 在应用程序开发中,会从代码以及 XML 文件中来访问定义好的资源
代码访问资源
Java 文字:txtName.setText(getResources().getText(R.string.name));
图片:imgIcon.setBackgroundDrawableResource(R.drawable.icon);
颜色:txtName.setTextColor(getResouces().getColor(R.color.red));
布局:setContentView(R.layout.main);
控件:txtName = (TextView)findViewById(R.id.txt_name);
- Android 应用程序被编译后,自动生成一个 R 文件类,其中包含了所有 res/ 目录下资源的 ID,可以使用 R 类,通过子类+资源名或者直接使用资源 ID 来访问资源。
- 实例1:访问 res/drawable/myimage.png,并将其设置到 ImageView 上,可以使用以下代码:
ImageView imageView = (ImageView) findViewById(R.id.myimageview); imageView.setImageResource(R.drawable.myimage);
- 第一行代码用 R.id.myimageview 来从布局文件中获取id定义为 myimageview 的 ImageView 控件,如下所示就是 res/layout 目录下的 activity_main.xml 文件的内容,可以为每个控件设置 id值,然后从代码中进行获取。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
- 第二行用 R.drawable.myimage 来获取在 res/ 的 drawable 子目录下名为 myimage 的图片。
实例2:如下所示,res/values/strings.xml 有如下定义:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello, World!</string> </resources>
- 可以在 ID 为 msg 的 TextView 对象上使用资源 ID 来设置文本,具体如下:
TextView msgTextView = (TextView) findViewById(R.id.msg); msgTextView.setText(R.string.hello);
- 实例3:如下所示定义的布局 res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a Button" /> </LinearLayout>
- 在 MainActivity 中的 onCreate() 方法中可以加载这个布局,如下所示:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); }
在 XML 中访问
通过@xxx即可得到,比如这里获取文本和图片:
<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background = "@drawable/img_back"/>
- 如下所示的 XML 资源文件 res/values/strings.xml,其中包含一个颜色资源和一个字符串资源 :
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="opaque_red">#f00</color> <string name="hello">Hello!</string> </resources>
- 在布局文件中可以使用这些资源来设置文本颜色和文本内容:
<?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/opaque_red" android:text="@string/hello" />