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

Dan*_*ele 5 android android-shortcut

我的应用有minSdk = 21和targetSdk = 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>
Run Code Online (Sandbox Code Playgroud)

然后我创建了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> 
Run Code Online (Sandbox Code Playgroud)

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

错误:错误:'long test'与属性android:shortcutLongLabel(attr)引用不兼容.错误:错误:'test'与属性android:shortcutShortLabel(attr)引用不兼容.错误:'long test'与属性android:shortcutLongLabel(attr)引用不兼容.

Dip*_* s. 8

希望这可以帮助.

strings.xml

 <string name="long_shortcut">long test</string>
  <string name="short_shortcut">test</string>
Run Code Online (Sandbox Code Playgroud)

使用此字符串的引用 Manifest.xml

<shortcut
        android:shortcutId="test"
        android:icon="@drawable/plus"
        android:shortcutShortLabel="@string/short_shortcut"
        android:shortcutLongLabel="@string/long_shortcut" >
Run Code Online (Sandbox Code Playgroud)

在Java中,你还可以通过使用分配此 setLongLabel (CharSequence longLabel)setShortLabel(CharSequence)ShortcutInfo.Builder.

  • Manifest更喜欢Project中对任何`ID`的引用(当使用fb_id,fabric_id或map_id时)/`String`(直接分配字符串时). (3认同)