【问题标题】:Android Studio - PreferenceScreen not 'previewing'Android Studio - PreferenceScreen 不是“预览”
【发布时间】:2018-09-17 06:16:43
【问题描述】:

我有一个全新安装的 Android Studio 3.1.4 我希望在我的应用中添加一个 PreferenceScreen(我只是按照教程来添加 设置面板)。我一步一步阅读了文档并进行了相应的编码,但我不明白为什么 Android Studio 不呈现 PreferenceScreen。 这是我的 PrefenceScreen 的代码:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:defaultValue="false"
        android:key="switch_preference_1"
        android:title="Switch preference" />
    <EditTextPreference
        android:defaultValue="Default value"
        android:key="edit_text_preference_1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="Edit text preference" />
</PreferenceScreen>

布局预览为空白:

preferences.xml 文件位于 \res\xml 目录中。

构建时没有错误/警告。

【问题讨论】:

    标签: android-preferences android-studio-3.0


    【解决方案1】:

    我今天遇到了同样的问题,但使用的是更新版本的 Android Studio。

    我正在使用 Android Studio 4.0.1 和 Android X 库。我在这里找到了相关问题的答案:https://stackoverflow.com/a/53615855/5152586,这表明使用首选项屏幕布局需要在 build.gradle 文件中添加首选项依赖项。

    implementation 'androidx.preference:preference:1.1.1'
    

    将依赖项添加到我的 build.gradle 文件对我有用,我现在可以在布局设计器模式下看到我的首选项屏幕的渲染预览。

    如果您还没有使用最新的东西,对同一问题的另一个答案 (https://stackoverflow.com/a/54738703/5152586) 表明您需要的依赖项将是支持库中的 AppCompat 和 Preference 依赖项。

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:preference-v7:28.0.0'
    

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多