如何在Android Studio中添加弹出菜单到Android桌面上的应用程序图标



如何在Android Studio中为Android桌面上的应用程序图标添加弹出菜单?

apk Chrome 的弹出菜单示例

AndroidManifest.xml中设置shortcuts.xml的资源。

就像你的<activity android:name="Main"><meta.data .../>一样

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application ... >
<activity android:name="Main">
<meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" /> 
</activity>
</application>
</manifest>

现在你需要定义shortcuts.xml本身(我用Chrome做了这个例子(:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="new_tab"
android:enabled="true"
android:icon="@drawable/new_tab_icon"
android:shortcutShortLabel="@string/new_tab_shortcut_short_label1"
android:shortcutLongLabel="@string/new_tab_shortcut_long_label1"
android:shortcutDisabledMessage="@string/new_tab_disabled_message1"> <!-- create a String in strings.xml -->
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.myapplication"
android:targetClass="com.example.myapplication.ChromeActivity" />
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list determines what the user sees when
they launch this shortcut. -->
<categories android:name="android.shortcut.new_tab" />
</shortcut>
<shortcut
android:shortcutId="new_incognito_tab"
android:enabled="true"
android:icon="@drawable/new_tab_incognito_icon"
android:shortcutShortLabel="@string/new_incognito_tab_shortcut_short_label1"
android:shortcutLongLabel="@string/new_incognito_tab_shortcut_long_label1"
android:shortcutDisabledMessage="@string/new_incognito_tab_disabled_message1">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.myapplication"
android:targetClass="com.example.myapplication.ChromeActivity" />
<categories android:name="android.shortcut.new_incognito_tab" />
</shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>

相关内容

最新更新