代码之家  ›  专栏  ›  技术社区  ›  wbk727

创建首选项屏幕时找不到androidx.preference.preference screen

  •  3
  • wbk727  · 技术社区  · 6 年前

    以下之后 this tutorial 要为首选项创建一个屏幕,似乎在向类“androidx.preference.preference screen”充气时出现问题。当我的首选项在 res/xml 文件夹和必要的依赖项是否已添加到此项目中?

    我的应用程序的minsdk版本是24。

    对类进行充气时出错(未找到)androidx.preference.preferencescreen

    依赖关系

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support:cardview-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:preference-v7:28.0.0'
        implementation 'com.android.support:recyclerview-v7:28.0.0'
        implementation 'com.android.support:support-v4:28.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    

    res/xml/preferences.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.preference.PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <CheckBoxPreference
            android:key="preference_a"
            android:defaultValue="false"
            android:title="Preference A" />
    
    </androidx.preference.PreferenceScreen>
    

    活动布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/settings_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MySettingsActivity" />
    

    活动课

    class MySettingsActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            supportFragmentManager
                    .beginTransaction()
                    .replace(R.id.settings_container, MySettingsFragment())
                    .commit()
        }
    }
    

    片段类

    class MySettingsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            addPreferencesFromResource(R.xml.app_preferences)
        }
    }
    
    3 回复  |  直到 5 年前
        1
  •  3
  •   shkschneider blazzerbg    6 年前

    如果您使用的是Androidx,那么应该更新您的依赖项:

    implementation "androidx.legacy:legacy-preference-v14:1.0.0"
    implementation "androidx.preference:preference:1.0.0"
    

    遗产是留给老人的 com.android.support:preference-v14 另一个是为了 com.android.support:preference-v7 .

    如果您不使用androidx但使用android支持库,请不要将androidx小部件导入XML。

        2
  •  0
  •   Hpsaturn    5 年前

    带支持库 28.0.0 您应该有这样的XML代码: ( 注释 去除 androidx 对于这种情况)

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
        <CheckBoxPreference
            android:key="preference_a"
            android:defaultValue="false"
            android:title="Preference A" />
    </PreferenceScreen>
    

    gradle 配置文件:

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

    实现方法如下: ( 注意 : set 而不是 add rootKey ):

    public class SettingsFragment extends PreferenceFragmentCompat {
    
        public static final String TAG = SettingsFragment.class.getSimpleName();
    
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.preferences, rootKey);
        }
    }
    

    oficial documentation 他们有 仙女座 在示例代码中,也许这就是问题所在,您不需要它与支持库一起使用。

        3
  •  0
  •   Abdurahman Popal    5 年前

    如果你想用 PreferenceFragmentCompat 您应该实现以下依赖项。

    implementation 'androidx.preference:preference:1.0.0