java - Androidx的PreferenceScreen使用方法

标签 java android xml android-preferences androidx

我的问题是我想向我的应用程序添加一个 PreferenceScreen,但我无法使用它,我也不知道为什么。

我实现了库 androidx.preference:preference:1.1.0-rc01。然后我想将 PreferenceScreen 添加到我的 XML 布局中,但它没有提出任何建议。

接下来,我将 Android-Developers 的 XML 代码复制到我的 XML 布局中并对其进行了编译,但是通过启动 Activity,它因错误而中断:java.lang.ClassCastException: class androidx.preference.PreferenceScreen无法转换为 android.view.View

有人可以帮助我正确使用 androidx.preference.PreferenceScreen 吗?

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <androidx.preference.SwitchPreference
        android:defaultValue="true"
        android:key="example_switch"
        android:summary="Turn this option on or off"
        android:title="Settings option" />
</androidx.preference.PreferenceScreen>

最佳答案

现在完整答案:

  1. Add this line to your App-Gradle: implementation 'androidx.preference:preference:1.1.1' or implementation 'androidx.preference:preference-ktx:1.1.1' for Kotlin. And sync Gradle. Create a Directory named xml in res Folder.

  2. Create in this directory an XML-File with your prefered name for example main_preferences. Root Element must be androidx.preference.PreferenceScreen.

  3. Fill XML-File with your settings for example:

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <androidx.preference.SwitchPreference
        android:defaultValue="true"
        android:key="example_switch"
        android:summary="Turn this option on or off"
        android:title="Settings option" />
</androidx.preference.PreferenceScreen>
  1. Create somewhere in the folder com.???.??? a java file for example named MainSettingsFragment. Superclass (means <Classname> extends <Superclass>) must be PreferenceFragmentCompat and override onCreatePreferences. You can copy this code:
import android.os.Bundle; 
import androidx.preference.PreferenceFragmentCompat;
import com.???.???.R;

public class <YourClassname> extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        // Load the preferences from an XML resource
        setPreferencesFromResource(R.xml.<yourXmlFilename>, rootKey);
    }
}
  1. Next, there are two options to implement the PreferencesSreen in your .

最好的方法是,在创建时按代码实现它。 添加您的 SettingsActivty一个FrameLayout并给它一个 ID,例如 fl_main_settings :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/<!-- yourID -->">

</FrameLayout>

然后在您的 Activity 代码中,在 onCreate 方法的顶部添加:

package com.???.???;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.MenuItem;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.text.HtmlCompat;

import com.???.???.MainSettingsFragment;
public class SettingsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.<SettingsActivityXML(with the FrameLayout)>);

        //If you want to insert data in your settings
        <YourSettingsFragmentClass> settingsFragment = new <YourSettingsFragmentClass>();
        settingsFragment. ...
        getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,settingsFragment).commit();
        
        //Else
        getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,new <YourSettingsFragmentClass>()).commit();
    }

或者您实现一个Fragment在您的 SettingsActivityXml 中。 但我不推荐这样做,因为启动 Activity 需要几秒钟:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">

    <fragment
        android:tag="frag"
        android:name="com.quickme.musicme.Fragments.MainSettingsFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

这就是它的乐趣。

关于java - Androidx的PreferenceScreen使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57466675/

相关文章:

java - 如何从非 Activity 的非 Activity 类获取 SharedPreference

android - 我想在使用 AChartEngine 时显示网格而不是标签

mysql - mybatis+spring xml sql 关键字 in 和 group by

android - 在运行时更改组件的 ID 名称

java - 使用 Java 桌面应用程序中的 API 更新 Dropbox(或 Drive)共享文件中的数据

java - 从文本文件 java 中分离字符串输入

java - 将一串多个句子拆分成单个句子,并用 html 标签包围它们

android - 如何检查 Whatsapp 是否安装在 android 设备中?

android - 关于图像和文件夹

xml - XPath 查询通过某种实现来选择元素