使用 androidx.preference.PreferenceScreen 和 PreferenceScreen 的区别
Difference between using androidx.preference.PreferenceScreen and PreferenceScreen
我的应用面向 API 28
并且至少有 API 15
。作为支持库,我使用 AndroidX
.
我有一个由 activity 托管的偏好片段,如下所示:
SettingsFragment.java
package com.example.app;
import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
SettingsActivity.java
package com.example.app;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
}
这是 SettingsFragment.java
使用的 XML 布局
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="false"
android:key="pref_switch"
android:title="@string/switch" />
</PreferenceScreen>
作为首选项层次结构的根,我应该使用 PreferenceScreen
还是 androidx.preference.PreferenceScreen
让布局真正向后兼容(使用 AndroidX)?两者有什么区别?最佳做法是什么?
来自the docs:
AndroidX is the open-source project that the Android team uses to
develop, test, package, version and release libraries within Jetpack.
AndroidX is a major improvement to the original Android Support
Library. Like the Support Library, AndroidX ships separately from the
Android OS and provides backwards-compatibility across Android
releases. AndroidX fully replaces the Support Library by providing
feature parity and new libraries. In addition AndroidX includes the
following features:
- All packages in AndroidX live in a consistent namespace starting with the string
androidx
. The Support Library packages have been
mapped into corresponding androidx.*
packages. For a full mapping of
all the old classes and build artifacts to the new ones, see the
Package Refactoring page.
简而言之,您应该使用新库而不是支持库,因为它具有最新的组件和功能。
因此,您的 PreferenceScreen
与 androidx.preference.PreferenceScreen
相同,但捆绑了不同的包装器。
我的应用面向 API 28
并且至少有 API 15
。作为支持库,我使用 AndroidX
.
我有一个由 activity 托管的偏好片段,如下所示:
SettingsFragment.java
package com.example.app;
import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
SettingsActivity.java
package com.example.app;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
}
这是 SettingsFragment.java
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="false"
android:key="pref_switch"
android:title="@string/switch" />
</PreferenceScreen>
作为首选项层次结构的根,我应该使用 PreferenceScreen
还是 androidx.preference.PreferenceScreen
让布局真正向后兼容(使用 AndroidX)?两者有什么区别?最佳做法是什么?
来自the docs:
AndroidX is the open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack.
AndroidX is a major improvement to the original Android Support Library. Like the Support Library, AndroidX ships separately from the Android OS and provides backwards-compatibility across Android releases. AndroidX fully replaces the Support Library by providing feature parity and new libraries. In addition AndroidX includes the following features:
- All packages in AndroidX live in a consistent namespace starting with the string
androidx
. The Support Library packages have been mapped into correspondingandroidx.*
packages. For a full mapping of all the old classes and build artifacts to the new ones, see the Package Refactoring page.
简而言之,您应该使用新库而不是支持库,因为它具有最新的组件和功能。
因此,您的 PreferenceScreen
与 androidx.preference.PreferenceScreen
相同,但捆绑了不同的包装器。