微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android – 尝试添加应用程序快捷方式时出错

我的应用有minSdk = 21和targetSdk = 26

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

添加了<元数据>标记应用程序启动时启动的第一个活动.

<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> 

当我尝试构建应用程序时,我收到以下错误

Error:error: ‘long test’ is incompatible with attribute
android:shortcutLongLabel (attr) reference.
Error:error: ‘test’ is incompatible with attribute android:shortcutShortLabel (attr) reference.
Error:’long test’ is incompatible with attribute android:shortcutLongLabel (attr) reference.

解决方法:

希望这可以帮助.

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.Builder的setLongLabel(CharSequence longLabel)和setShortLabel(CharSequence)来分配它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐