
res\values\strings.xml可用来配置字符串形式的参数。
配置的字符串参数例子如下:
在活动页面的Java代码中,调用getString方法即可根据“R.string.参数名称”获得指定参数的字符串值。
获取代码示例如下:
private void showStringResource() // 显示字符串资源
{ String value = getString(R.string.weather_str); // 从strings.xml获取名叫weather_str的字符串值 tv_resource.setText("来自字符串资源:今天的天气是"+value); // 在文本视图上显示文字 }
strings.xml
- <resources>
- <string name="app_name">My Applicationstring>
- <string name="hello">中国,您好string>
- <string name="weather_str">今天,温度高string>
- resources>

布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/tv_resource"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:padding="5dp"
- android:gravity="center"
- android:textColor="#000000"
- android:textSize="17sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
-
- public class ReadStringActivity extends AppCompatActivity
- {
- private TextView tv_resource; // 声明一个文本视图对象
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_read_string);
-
-
-
- tv_resource = findViewById(R.id.tv_resource); // 从布局文件中获取名叫tv_resource的文本视图
-
- showStringResource(); // 显示字符串资源
- }
-
- // 显示字符串资源
- private void showStringResource()
- {
- String value = getString(R.string.weather_str); // 从strings.xml获取名叫weather_str的字符串值
-
- tv_resource.setText("来自字符串资源:今天的天气是: "+value); // 在文本视图上显示文字
- }
- }


============================================================================================

元数据是一种描述其他数据的数据,它相当于描述固定活动的参数信息。
在activity节点内部添加meta-data标签,通过属性name指定元数据的名称,通过属性value指定元数据的值。
示例如下:
也可引用strings.xml已定义的字符串资源,举例如下:

在Java代码中,获取元数据信息的步骤分为下列三步:
(1)调用getPackageManager方法获得当前应用的包管理器;
(2)调用包管理器的getActivityInfo方法获得当前活动的信息对象;
(3)活动信息对象的metaData是Bundle包裹类型,调用包裹对象的getString即可获得指定名称的参数值;
布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/tv_meta"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:padding="5dp"
- android:gravity="center"
- android:textColor="#000000"
- android:textSize="17sp" />
-
- LinearLayout>

代码:
- package com.example.myapplication;
-
- import android.content.pm.ActivityInfo;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
-
- public class MetaDataActivity extends AppCompatActivity
- {
- private TextView tv_meta; // 声明一个文本视图对象
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_meta_data);
-
- // 从布局文件中获取名叫tv_meta的文本视图
- tv_meta = findViewById(R.id.tv_meta);
-
- showMetaData(); // 显示配置的元数据
- }
-
- // 显示配置的元数据
- private void showMetaData()
- {
- try
- {
- PackageManager pm = getPackageManager(); // 获取应用包管理器
-
- // 从应用包管理器中获取当前的活动信息
- ActivityInfo act = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
-
- Bundle bundle = act.metaData; // 获取活动附加的元数据信息
-
- String value = bundle.getString("weather"); // 从包裹中取出名叫weather的字符串
-
- tv_meta.setText("来自元数据信息:今天的天气是: "+value); // 在文本视图上显示文字
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }


strings.xml
- <resources>
- <string name="app_name">My Applicationstring>
- <string name="hello">中国,您好string>
- <string name="weather_str">今天,温度高string>
- resources>


- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- package="com.example.myapplication">
-
- <application
- android:allowBackup="true"
- android:dataExtractionRules="@xml/data_extraction_rules"
- android:fullBackupContent="@xml/backup_rules"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.MyApplication"
- tools:targetApi="31">
- <activity android:name=".MetaDataActivity">
-
- <meta-data
- android:name="weather"
- android:value="@string/weather_str" />
- activity>
- <activity
- android:name=".ReadStringActivity"
- android:exported="false" />
- <activity
- android:name=".ActionUriActivity"
- android:exported="false" />
- <activity
- android:name=".ActResponseActivity"
- android:exported="false" />
- <activity
- android:name=".ActRequestActivity"
- android:exported="false" />
- <activity
- android:name=".ActReceiveActivity"
- android:exported="false" />
- <activity
- android:name=".ActSendActivity"
- android:exported="false" />
- <activity
- android:name=".LoginSuccessActivity"
- android:exported="false" />
- <activity
- android:name=".LoginInputActivity"
- android:exported="false" />
- <activity
- android:name=".ActNextActivity"
- android:exported="false" />
- <activity
- android:name=".NextActivity"
- android:exported="false" />
- <activity
- android:name=".MainActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- intent-filter>
- activity>
- application>
-
- manifest>
另外:

- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- package="com.example.myapplication">
-
- <application
- android:allowBackup="true"
- android:dataExtractionRules="@xml/data_extraction_rules"
- android:fullBackupContent="@xml/backup_rules"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.MyApplication"
- tools:targetApi="31">
- <activity android:name=".MetaDataActivity">
- <meta-data android:name="weather" android:value="晴天" />
- activity>
- <activity
- android:name=".ReadStringActivity"
- android:exported="false" />
- <activity
- android:name=".ActionUriActivity"
- android:exported="false" />
- <activity
- android:name=".ActResponseActivity"
- android:exported="false" />
- <activity
- android:name=".ActRequestActivity"
- android:exported="false" />
- <activity
- android:name=".ActReceiveActivity"
- android:exported="false" />
- <activity
- android:name=".ActSendActivity"
- android:exported="false" />
- <activity
- android:name=".LoginSuccessActivity"
- android:exported="false" />
- <activity
- android:name=".LoginInputActivity"
- android:exported="false" />
- <activity
- android:name=".ActNextActivity"
- android:exported="false" />
- <activity
- android:name=".NextActivity"
- android:exported="false" />
- <activity
- android:name=".MainActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- intent-filter>
- activity>
- application>
-
- manifest>

========================================================================================

元数据的meta-data标签除了前面说到的name属性和value属性,还拥有resource属性,该属性可指定一个XML文件,表示元数据想要的复杂信息保存于XML数据之中。
利用元数据配置快捷菜单的步骤如下所示:
(1)在res/values/strings.xml添加各个菜单项名称的字符串配置
(2)创建res/xml/shortcuts.xml,在该文件中填入各组菜单项的快捷方式定义(每个菜单对应哪个活动页面)。
(3)给activity节点注册元数据的快捷菜单配置,举例如下:

- 完整的activity节点配置示例如下:
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- intent-filter>
-
- <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
- activity>
布局:
shortcuts.xml
- <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
- <shortcut
- android:shortcutId="first"
- android:enabled="true"
- android:icon="@mipmap/ic_launcher"
- android:shortcutShortLabel="@string/first_short"
- android:shortcutLongLabel="@string/first_long">
-
- <intent
- android:action="android.intent.action.VIEW"
- android:targetPackage="com.example.myapplication"
- android:targetClass="com.example.myapplication.ActSendActivity" />
- <categories android:name="android.shortcut.conversation"/>
- shortcut>
-
- <shortcut
- android:shortcutId="second"
- android:enabled="true"
- android:icon="@mipmap/ic_launcher"
- android:shortcutShortLabel="@string/second_short"
- android:shortcutLongLabel="@string/second_long">
-
- <intent
- android:action="android.intent.action.VIEW"
- android:targetPackage="com.example.myapplication"
- android:targetClass="com.example.myapplication.ActionUriActivity" />
- <categories android:name="android.shortcut.conversation"/>
- shortcut>
-
- <shortcut
- android:shortcutId="third"
- android:enabled="true"
- android:icon="@mipmap/ic_launcher"
- android:shortcutShortLabel="@string/third_short"
- android:shortcutLongLabel="@string/third_long">
-
- <intent
- android:action="android.intent.action.VIEW"
- android:targetPackage="com.example.myapplication"
- android:targetClass="com.example.myapplication.LoginInputActivity" />
- <categories android:name="android.shortcut.conversation"/>
- shortcut>
- shortcuts>


strings.xml
- <resources>
- <string name="app_name">My Applicationstring>
- <string name="hello">中国,您好string>
- <string name="weather_str">今天,温度高string>
- <string name="first_short">firststring>
- <string name="first_long">启停活动string>
- <string name="second_short">secondstring>
- <string name="second_long">来回跳转string>
- <string name="third_short">thirdstring>
- <string name="third_long">登录返回string>
- resources>

- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- package="com.example.myapplication">
-
- <application
- android:allowBackup="true"
- android:dataExtractionRules="@xml/data_extraction_rules"
- android:fullBackupContent="@xml/backup_rules"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.MyApplication"
- tools:targetApi="31">
- <activity android:name=".MetaDataActivity">
- <meta-data android:name="weather" android:value="晴天" />
- activity>
- <activity
- android:name=".ReadStringActivity"
- android:exported="false" />
- <activity
- android:name=".ActionUriActivity"
- android:exported="false" />
- <activity
- android:name=".ActResponseActivity"
- android:exported="false" />
- <activity
- android:name=".ActRequestActivity"
- android:exported="false" />
- <activity
- android:name=".ActReceiveActivity"
- android:exported="false" />
- <activity
- android:name=".ActSendActivity"
- android:exported="false" />
- <activity
- android:name=".LoginSuccessActivity"
- android:exported="false" />
- <activity
- android:name=".LoginInputActivity"
- android:exported="false" />
- <activity
- android:name=".ActNextActivity"
- android:exported="false" />
- <activity
- android:name=".NextActivity"
- android:exported="false" />
- <activity
- android:name=".MainActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- intent-filter>
-
- <meta-data
- android:name="android.app.shortcuts"
- android:resource="@xml/shortcuts" />
- activity>
- application>
-
- manifest>








PS:示例
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.chapter04">
-
- <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/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- intent-filter>
-
- <meta-data
- android:name="android.app.shortcuts"
- android:resource="@xml/shortcuts" />
- activity>
- <activity android:name=".ActStartActivity" />
- <activity android:name=".ActFinishActivity" />
- <activity android:name=".ActLifeActivity" />
- <activity android:name=".ActNextActivity" />
- <activity
- android:name=".JumpFirstActivity"
- android:launchMode="standard" />
- <activity android:name=".JumpSecondActivity" />
- <activity android:name=".LoginInputActivity" />
- <activity android:name=".LoginSuccessActivity" />
- <activity android:name=".ActionUriActivity" />
- <activity android:name=".ActSendActivity" />
- <activity android:name=".ActReceiveActivity" />
- <activity android:name=".ActRequestActivity" />
- <activity android:name=".ActResponseActivity" />
- <activity android:name=".ReadStringActivity" />
- <activity android:name=".MetaDataActivity">
-
-
- <meta-data
- android:name="weather"
- android:value="@string/weather_str" />
- activity>
- application>
-
- manifest>

