我的问题是我想向我的应用程序添加一个 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>
最佳答案
现在完整答案:
Add this line to your App-Gradle:
implementation 'androidx.preference:preference:1.1.1'
orimplementation 'androidx.preference:preference-ktx:1.1.1'
for Kotlin. And sync Gradle. Create a Directory namedxml
in res Folder.Create in this directory an XML-File with your prefered name for example
main_preferences
. Root Element must beandroidx.preference.PreferenceScreen
.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>
- Create somewhere in the folder com.???.??? a java file for example named
MainSettingsFragment
. Superclass (means<Classname> extends <Superclass>
) must bePreferenceFragmentCompat
and overrideonCreatePreferences
. 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);
}
}
- 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/