资源文件的存放位置
首先来看一下Android工程的目录结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| MyProject/ manifests/ AndroidManifest.xml java/ host.skyone.resource/ MainActivity.java res/ anim/ color/ drawable/ skyone.png layout/ activity_main.xml menu/ xml/ raw/ mipmap/ icon.png values/ strings.xml
|
目录 |
资源类型 |
anim/ |
定义动画属性的XML文件。它们被保存在res/anim/ 文件夹下,通过R.anim 类访问 |
color/ |
定义颜色状态列表的XML文件。它们被保存在res/color/文件夹下,通过R.color 类访问 |
drawable/ |
图片文件,如.png ,.jpg ,.gif 或者XML 文件,被编译为位图、状态列表、形状、动画图片。它们被保存在res/drawable/ 文件夹下,通过R.drawable 类访问 |
layout/ |
定义用户界面布局的XML文件。它们被保存在res/layout/文件夹下,通过R.layout 类访问 |
menu/ |
定义应用程序菜单的XML文件,如选项菜单,上下文菜单,子菜单等。它们被保存在res/menu/ 文件夹下,通过R.menu 类访问 |
raw/ |
任意的文件以它们的原始形式保存。需要根据名为R.raw.filename 的资源ID,通过调用Resource.openRawResource() 来打开raw 文件 |
values/ |
包含简单值(如字符串,整数,颜色等)的XML文件。这里有一些文件夹下的资源命名规范。arrays.xml代表数组资源,通过R.array 类访问;integers.xml 代表整数资源,通过R.integer 类访问;bools.xml 代表布尔值资源,通过R.bool 类访问;colors.xml 代表颜色资源,通过R.color 类访问;dimens.xml 代表维度值,通过R.dimen 类访问;strings.xml 代表字符串资源,通过R.string 类访问;styles.xml 代表样式资源,通过R.style 类访问 |
xml/ |
可以通过调用Resources.getXML() 来在运行时读取任意的XML文件。可以在这里保存运行时使用的各种配置文件 |
minmap/ |
保存应用的图标 |
简单的例子
配置资源
例如:将以下图片下载并命名为skyone.png
移动到res/drawable/
文件夹下,在activity_main.xml
中加入ImageView
1 2 3 4 5 6 7 8 9
| <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="180dp" android:contentDescription="@string/img_skyone" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/skyone" />
|
在res/values/strings.xml
中加入:
1
| <string name="img_skyone">Skyone</string>
|
连接手机,编译运行。
代码
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="180dp" android:contentDescription="@string/img_skyone" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/skyone" />
<Button android:id="@+id/button" android:layout_width="100dp" android:layout_height="100dp" android:background="#FFFFFF" android:onClick="onClickButton" android:text="@string/button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package host.skyone.resource;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.view.View; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
public void onClickButton(View view) { Toast.makeText(this, "欢迎访问Skyone哦!" , Toast.LENGTH_LONG).show(); } }
|
strings.xml
1 2 3 4 5
| <resources> <string name="app_name">Resource</string> <string name="img_skyone">Skyone</string> <string name="button">点我~^_^~</string> </resources>
|
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="host.skyone.resource">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Resource"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
|
例图: