
- 这里为了省事,只定义了三个字符串
- <resources>
- <string name="app_name">支付宝</string>
- <string name="sao">扫一扫</string>
- <string name="shou">收钱</string>
- <string name="fu">付钱</string>
- <string name="title_activity_main2">Main2Activity</string>
- </resources>
2. 接着在res目录下创建名为xml的文件夹,并在该文件夹创建shortcuts.xml,这个XML文件用来保存3组 菜单项的快捷方式定义,文件内容如下所示:(注意,文件夹名和文件名一字不能错!)
- <?xml version="1.0" encoding="utf-8"?>
- <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
- <shortcut
- android:shortcutId="first" //设置id,是快捷方式的编号
- android:enabled="true" //是否启用快捷方式,如果为false则不会显示
- android:icon="@drawable/sao" //图标的位置
- android:shortcutShortLabel="@string/sao" //快捷菜单短标签
- android:shortcutLongLabel="@string/sao"> //快捷菜单长标签
- <intent //Intent必须要设置
- android:action="android.intent.action.VIEW" //基础操作
- android:targetPackage="com.example.helloworld" //目标包名
- android:targetClass="com.example.helloworld.MainActivity"/>
- //targetclass指定点击该项菜单后打开哪个活动页面
- <categories android:name="android.shortcut.conversation" /> //官方提供的
- </shortcut>
- <shortcut
- android:shortcutId="second"
- android:enabled="true"
- android:icon="@drawable/fu"
- android:shortcutShortLabel="@string/shou"
- android:shortcutLongLabel="@string/shou">
- <intent
- android:action="android.intent.action.VIEW"
- android:targetPackage="com.example.helloworld"
- android:targetClass="com.example.helloworld.MainActivity"/>
- <categories android:name="android.shortcut.conversation" />
- </shortcut>
- <shortcut
- android:shortcutId="third"
- android:enabled="true"
- android:icon="@drawable/fu"
- android:shortcutShortLabel="@string/fu"
- android:shortcutLongLabel="@string/fu">
- <intent
- android:action="android.intent.action.VIEW"
- android:targetPackage="com.example.helloworld"
- android:targetClass="com.example.helloworld.MainActivity"/>
- <categories android:name="android.shortcut.conversation" />
- </shortcut>
- </shortcuts>
3. 然后打开AndroidManifest.xml,找到MainActivity所在的activity节点,在该节点内部补充如下的元数据配置,其中name属性为android.app.shortcuts,而resource属性为@xml/shortcuts。这行元数据的作用,是告诉App首页有个快捷方式菜单,其资源内容参见位于xml目录下shortcuts.xml。完整的activity节点配置示例如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.helloworld">
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true">
- <activity
- android:name=".Main2Activity"
- android:theme="@style/AppTheme.NoActionBar" />
- <activity
- android:name=".MainActivity"
- android:launchMode="standard"
- android:theme="@style/Theme.AppCompat.Light"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <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>