如何使用 Androidx 的 PreferenceScreen

Bie*_*Dav 15 java xml android android-preferences androidx

我的问题是我想在我的 Application 中添加一个 PreferenceScreen,但我不能使用它,我不知道为什么。

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

接下来,我将来自 Android-Developers 的 XML 代码复制到我的 XML 布局中并对其进行编译,但是通过启动活动,它因错误而中断: java.lang.ClassCastException: class androidx.preference.PreferenceScreen cannot be cast to 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>
Run Code Online (Sandbox Code Playgroud)

Bie*_*Dav 26

现在整个答案:

  1. 将此行添加到您的 App-Gradle:implementation 'androidx.preference:preference:1.1.1'implementation 'androidx.preference:preference-ktx:1.1.1'Kotlin。并同步 Gradle。xml在 res 文件夹中创建一个目录。

  2. 在此目录中使用您喜欢的名称创建一个 XML 文件,例如main_preferences。根元素必须是 androidx.preference.PreferenceScreen.

  3. 使用您的设置填充 XML 文件,例如:

<?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>
Run Code Online (Sandbox Code Playgroud)
  1. 在文件夹 com.???.??? 中的某处创建 一个 java 文件,例如名为MainSettingsFragment. 超类(意味着<Classname> extends <Superclass>)必须是PreferenceFragmentCompat和覆盖 onCreatePreferences。您可以复制此代码:
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);
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 接下来,有两个选项可以在您的 .

美丽和最好的方法是,它在创建时按代码实现它。添加您的SettingsActivtyaFrameLayout并为其指定一个 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>
Run Code Online (Sandbox Code Playgroud)

在您的活动代码中,在 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();
    }
Run Code Online (Sandbox Code Playgroud)

或者Fragment在 SettingsActivityXml 中实现一个。但我不建议这样做,因为启动活动需要几秒钟

<?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>
Run Code Online (Sandbox Code Playgroud)

就这样玩得很开心。

  • 谢谢你的这个教程,非常清楚。但是,当您说(第 5 点)“添加到您的 FrameLayout 中”时,您能说得更清楚吗?您错过了一个单词,不是吗?是在你的 main_activity.xml 中添加一个 FrameLayout 吗???或者是在另一个 xml 文件中? (2认同)