一、字符串资源
- 为了改善程序的可读性和维护成本,Android允许把应用中用到的各种资源:字符串资源、颜色资源、数据资源等集中放到res目录中定义,应用程序则可以直接使用这些资源定义的值。
- 字符串资源文件位于res/values目录下,根元素是< resources >< /resources >标记,在该元素中,使用< string >< /string >标记定义各字符串,其中,通过为< string >< /string >标记设置name属性来指定字符串的名称,在其实标记< string >和< /string >中间添加字符串内容。
提示使用用户名资源来完成。
步骤
在strings.xml文件中补充——
在activity_main.xml中加入——
完整的修改情况如下——
strings.xml:
<resources>
<string name="app_name">My Application</string>
<string name="user">用户名</string>
<string name="password">密码</string>
<string name="login">登录</string>
<string name="cancel">取消</string>
</resources>
activity_main.xml
<?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">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.205"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.442" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.216"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.651"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.434" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.651"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.343"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.721"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
</androidx.constraintlayout.widget.ConstraintLayout>
二、颜色资源
- 颜色资源文件位于res/values目录下,根元素是< resources >< /resources >标记,在该元素中,使用< color >< /color >标记定义各颜色资源,其中,通过为< color >< /color >标记设置name属性来指定颜色资源的名称,在其实标记< color >和< /color >中间添加颜色值。
步骤
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="textColor">#1f72cd</color>
<color name="btnColor">#13227a</color>
</resources>
在activity_main.xml对应位置引用,如——
android:textColor="@color/btnColor"
三、尺寸资源
- 颜色资源文件位于res/values目录下,根元素是< resources >< /resources >标记,在该元素中,使用< dimen >< /dimen >标记尺寸资源,其中,通过为< dimen >< /dimen >标记设置name属性来指定尺寸资源的名称,在其实标记< dimen >和< /dimen >中间添加定义一个尺寸常量。
- 尺寸资源是没有定义的,需要自己写一个文件。
步骤
dimen.xml——
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textSize">20dp</dimen>
<dimen name="btnSize0">33dp</dimen>
</resources>
在activity_main.xml对应位置引用,如——
android:textSize="@dimen/textSize"
四、数据资源
- 数组资源文件位于res/values目录下,根元素是< resources >< /resources >标记,在该元素中,包括以下三个子元素:
< arrary / >子元素 | 用于定义普通类型的数组 |
---|---|
< integer-array / > 子元素 | 用于定义整数数组 |
< string-array / >子元素 | 用于定义字符串数组 |
- 可以定义在strings.xml文件中
引用——可以通过Spinner引用
在Spinner种引用——
android:entries="@array/item"