安卓 - 尝试添加应用程序快捷方式时出现错误

9

我的应用的最小sdk版本为21,目标sdk版本为26

我想设置应用程序快捷方式

我已经将<meta-data>标签添加到应用启动时首个活动中。

<activity android:name=".SignInActivity"
            android:theme="@style/SubscriptionsTheme.NoActionBar">
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.MAIN"/>
            </intent-filter>
            <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
        </activity>

接着我创建了xml-v25文件夹,并在其中创建了shortcuts.xml文件:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
    <shortcut
        android:shortcutId="test"
        android:icon="@drawable/plus"
        android:shortcutShortLabel="test"
        android:shortcutLongLabel="long test" >

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.xxxxx.xxxxx.xxxxx"
            android:targetClass="com.xxxxx.xxxxx.xxxxxx.main" />

    </shortcut>
</shortcuts> 

当我尝试构建应用程序时,出现以下错误:
错误:'long test'与属性android:shortcutLongLabel(attr)引用不兼容。 错误:'test'与属性android:shortcutShortLabel(attr)引用不兼容。 错误:'long test'与属性android:shortcutLongLabel(attr)引用不兼容。

5
你可能需要使用一个字符串资源。 - Zoe stands with Ukraine
1个回答

13

希望这能有所帮助。

strings.xml

 <string name="long_shortcut">long test</string>
  <string name="short_shortcut">test</string>

Manifest.xml中引用这些字符串

<shortcut
        android:shortcutId="test"
        android:icon="@drawable/plus"
        android:shortcutShortLabel="@string/short_shortcut"
        android:shortcutLongLabel="@string/long_shortcut" >

在Java中,您还可以使用ShortcutInfo.BuildersetLongLabel(CharSequence longLabel)setShortLabel(CharSequence)来指定此选项的长短标签。


谢谢你的回答。这个方法可行。虽然我不明白为什么我不能硬编码它们,但至少可以用于测试。 - Daniele
3
在处理 fb_idfabric_idmap_id 时,Manifest 更倾向于使用 Project 中的引用作为任何 ID 的参考;而在直接分配字符串时,则更倾向于使用 String - Dipali Shah
短标签(shortLable)和长标签(longLable)有什么区别? - Bulma
@miho39,“android:shortcutLongLabel”和“android:shortcutShortLabel”都是向用户显示快捷方式名称的标签。建议短标签少于或等于10个字符,长标签少于或等于25个字符。Android将根据可用空间选择要显示的适当标签,但通常在主屏幕上放置时使用短标签。只要应用程序快捷菜单中有足够的空间,通常就会使用长标签。 https://medium.com/@andreworobator/implementing-android-app-shortcuts-74feb524959b - Dipali Shah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接