Preference的作用
Android Settings应用中绝大部分的页面都是由一个又一个的Preference构成的,那Preference究竟是什么呢?
我们来看google android开发者网站中对preference的描述:
developer.android.google.cn/guide/topic…
The recommended way to integrate user configurable settings into your application is to use the AndroidX Preference Library. This library manages the user interface and interacts with storage so that you define only the individual settings that the user can configure. The library comes with a Material theme that provides a consistent user experience across devices and OS versions.
建议使用 AndroidX Preference Library 将用户可配置设置集成至您的应用中。此库管理界面,并与存储空间交互,因此您只需定义用户可以配置的单独设置。此库自带 Material 主题,可在不同的设备和操作系统版本之间提供一致的用户体验。
再来看下Preference.java对类的描述:
The basic building block that represents an individual setting displayed to a user in the preference hierarchy. This class provides the data that will be displayed to the user and has a reference to the {@link SharedPreferences} or {@link PreferenceDataStore} instance that persists the preference's values.
When specifying a preference hierarchy in XML, each element can point to a subclass of {@link Preference}, similar to the view hierarchy and layouts.
This class contains a {@code key} that that represents the key that is used to persist the value to the device. It is up to the subclass to decide how to store the value.
表示在首选项层次结构中向用户显示的单个设置的基本构建块。 此类提供将显示给用户的数据,并具有对保留首选项值的 {@link SharedPreferences} 或 {@link PreferenceDataStore} 实例的引用。
在 XML 中指定首选项层次结构时,每个元素都可以指向 {@link Preference} 的子类,类似于视图层次结构和布局。
此类包含一个 {@code key},它表示用于将值保存到设备的键。 由子类决定如何存储值。
其实Preference不是Settings应用独有的,是google为了使应用能够快速搭建页面、保存和展示用户的数据并且能够统一用户体验,从而创造出来的一个类。
自 Android 10 开始,系统已弃用 android.preference 库平台。改为使用AndroidX Preference Library,是Android Jetpack的一部分。
应用怎么使用Preference
上面有说到Preference不是设置应用独有的,只要添加AndroidX Preference Library的依赖,都能使用Preference搭建页面。下面就用一个简单的例子来展示怎么使用Preference。(注意从Preference到Framgnet再到Activity都需要配套使用AndroidX)
build.gradle添加依赖
dependencies {
def preference_version = "1.2.0"
// Java language implementation
implementation "androidx.preference:preference:$preference_version"
}
PreferenceFragmentCompat.java
import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
public class MyPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.my_preference_layout);
}
}
my_preference_layout.xml(放到res/xml/目录下)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="copyright"
android:title="copyright" />
</PreferenceScreen>
MainActivity.java
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.appbar.CollapsingToolbarLayout;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new MyPreferenceFragment());
fragmentTransaction.commit();
}
}
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
好了,看下运行效果。
界面上有了copyright 这个item。