首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Androidx的PreferenceScreen

如何使用Androidx的PreferenceScreen
EN

Stack Overflow用户
提问于 2019-08-12 18:48:57
回答 3查看 15.9K关注 0票数 18

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

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

接下来,我将XML-Code从Android开发人员复制到XML布局中并对其进行编译,但是通过启动活动它就会中断错误:java.lang.ClassCastException: class androidx.preference.PreferenceScreen cannot be cast to android.view.View

有人能帮我正确使用androidx.preference.PreferenceScreen吗?

我的布局:

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-08-13 07:30:21

现在整个答案是:

  1. 将这一行添加到您的App:implementation 'androidx.preference:preference:1.1.1'implementation 'androidx.preference:preference-ktx:1.1.1'中。和同步格莱德尔。在res文件夹中创建名为xml的目录。
  2. 在这个目录中创建一个具有您首选名称的XML-文件,例如main_preferences。根元素必须是androidx.preference.PreferenceScreen
  3. 用您的设置填充XML--例如:
代码语言:javascript
复制
<?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. 在文件夹中创建某个位置。?例如,一个名为MainSettingsFragment的java文件。超类(意为<Classname> extends <Superclass>)必须是PreferenceFragmentCompat并覆盖onCreatePreferences。您可以复制此代码:
代码语言:javascript
复制
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. 接下来,有两个选项可以实现您的PreferencesSreen。

最美妙和最好的方法是,它在创建时根据代码实现它。在SettingsActivty中添加一个FrameLayout并给它一个ID,例如fl_main_settings

代码语言:javascript
复制
<?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方法的顶部添加以下内容:

代码语言:javascript
复制
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();
    }

--在SettingsActivityXml中实现Fragment,但我不推荐这样做,因为启动该活动需要几秒钟

代码语言:javascript
复制
<?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>

就是这样玩得开心。

票数 34
EN

Stack Overflow用户

发布于 2019-08-12 21:24:30

java.lang.ClassCastException:无法将类androidx.preference.PreferenceScreen转换为android.view.View 我的布局:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen>
...
</androidx.preference.PreferenceScreen>

注意了。

这是而不是布局。

有人能帮我正确使用androidx.preference.PreferenceScreen吗?

你可以找到所有的这里的信息

可以定义首选项层次结构。

警告:根标记必须是a <PreferenceScreen>,而XML资源必须放在中。

代码语言:javascript
复制
<PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">

    ....

</PreferenceScreen>

若要从XML属性中膨胀层次结构,请创建PreferenceFragmentCompat、重写onCreatePreferences()并提供XML资源:

代码语言:javascript
复制
class MySettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, rootKey)
    }
}

然后将这个Fragment添加到您的Activity中,就像添加任何其他“框架”一样。

票数 0
EN

Stack Overflow用户

发布于 2019-08-12 19:06:07

尝试: xml/preferences.xml

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

我也更喜欢使用由我的活动托管的偏好片段。

代码语言:javascript
复制
class MySettingsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings_container, SettingsFragment())
                .commit()
    }
}
代码语言:javascript
复制
class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, rootKey)
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57466675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档