Android Studio 无法识别 xml 中的 androidx 首选项库

标签 android xml android-preferences androidx

我正在尝试使用新的 androidx 首选项库,但 Android Studio 根本无法在 xml 中识别它。在我的 res/xml/setting_prefs.xml 中,我有

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:key="@string/settings">
    <androidx.preference.SwitchPreference android:key="test"
        android:title="test"/>
    <androidx.preference.SeekBarPreference
        android:title="Stopwatch frequency"
        android:summary="The time (in ms) between each update of the stopwatch"
        android:defaultValue="100"
        android:min="50"
        android:max="200"/>
</androidx.preference.PreferenceScreen>

虽然这在运行时工作正常,但 android studio 根本无法识别任何内容并且不提供自动完成建议。

我在build.gradle文件中添加了相关的依赖:

implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
implementation 'androidx.preference:preference:1.1.0-alpha02'

android studio 设置为使用 androidx

android.enableJetifier=true
android.useAndroidX=true

(注意这是一个全新的Androidx项目,不是我迁移的旧项目,我是在开始一个新项目后立即迁移到androidx)

我正在使用 Android Studio 3.2.1 并且已经尝试过常见的解决方案,例如使缓存无效+重新启动,删除 .idea 文件夹,删除依赖项并再次添加它们(甚至更改版本),删除 xml 文件并创建一个新文件,甚至尝试运行干净的 gradle 构建。它唯一识别或提供建议的是 android.preference.PreferenceScreen 类和默认(非 androidx 库)中的一些其他类。请注意,在 java 代码中可以毫无问题地识别 androidx 首选项类。

最佳答案

只需使用 <PreferenceScreen>标记而不是 androidx.preference.PreferenceScreen .

这是一个bug,希望尽快解决。确保<PreferenceScreen>没有出现任何问题,只需在完成使用 xml 文件后重命名即可。

关于Android Studio 无法识别 xml 中的 androidx 首选项库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54039525/

相关文章:

android - 工具栏箭头在 NavigationDrawer 的 PreferenceFragment 中不起作用

android - Android SDK 是否弃用了 ParseCrashReporting?

XML 命名空间前缀不能为空

java - 当我尝试从 GitHub 存储库构建和运行代码时出现 Gradle 同步问题

r - 在 R 中以 XML 格式编译 API 输出

android - 有人可以更改我的应用程序的首选项吗?

php - 安卓+php mysql

android - 在 Android 中使用 viewpager 进行图像滑动

android - 无法在 Android Studio 中运行后端

android - 从另一个 Activity 访问一个类