首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何使用安卓的DropDownPreference?

我如何使用安卓的DropDownPreference?
EN

Stack Overflow用户
提问于 2017-06-09 22:46:16
回答 2查看 7.8K关注 0票数 1

我正在尝试用DropDownPreference构建一个偏好屏幕。最初,我在我的gradle文件compile 'com.android.support:preference-v14:25.3.1'中使用了下面的内容,但当我注意到DropDownPreference包含在v7中而不是v14中时,我切换到了DropDownPreference(我以为v14可能也包括v7中的所有内容,但我想不是吗?)我的XML如下所示:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/pref_title"
android:layout_height="match_parent"
android:layout_width="match_parent">

    <PreferenceCategory
        android:key="pref_video"
        android:title="@string/pref_video_title">

        <android.support.v7.preference.DropDownPreference
            android:key="pref_video_quality"
            android:title="@string/pref_video_quality"
            android:summary="@string/pref_summary_video_quality"
            android:entries="@array/pref_entries_video_quality"
            android:entryValues="@array/pref_entries_video_quality" />

    </PreferenceCategory>

</PreferenceScreen>

我也刚刚尝试过DropDownPreference作为标记。似乎什么都起不到作用。当我试图进入应用程序的首选屏幕时,我总是会收到一个Error inflating class DropDownPreference错误。

知道我怎么用这个DropDownPreference吗?谢谢!

编辑:添加错误消息:

代码语言:javascript
运行
复制
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.int_a.giantbombforandroid/com.app.int_a.giantbombforandroid.main.SettingsActivity}: java.lang.ClassCastException: android.support.v7.preference.DropDownPreference cannot be cast to android.preference.Preference

编辑: SettingsActivity声明在AndroidManifest.xml中

代码语言:javascript
运行
复制
<activity android:name=".main.SettingsActivity"
  android:configChanges="orientation|screenSize"
  android:theme="@style/PreferenceThemeOverlay.v14.Material">
</activity>
EN

Stack Overflow用户

回答已采纳

发布于 2017-06-12 22:39:48

跟进我的评论。代码以Kotlin btw :)

styles.xml

代码语言:javascript
运行
复制
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>

activity_settings.xml

代码语言:javascript
运行
复制
<LinearLayout android:orientation="vertical">

    <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" />

    <FrameLayout android:id="@+id/frame"/>

</LinearLayout>

prefs.xml正是你的问题所在

Settings.kt

代码语言:javascript
运行
复制
class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        setupActionBar()

        if (savedInstanceState == null)
            supportFragmentManager
                    .beginTransaction()
                    .add(R.id.frame, SettingsFragment.newInstance())
                    .commit()
    }

    private fun setupActionBar() {
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    class SettingsFragment : PreferenceFragmentCompat() {

        override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)

            val actionBar = (activity as AppCompatActivity).supportActionBar

            actionBar?.title = preferenceScreen.title
        }

        override fun onCreatePreferences(bundle: Bundle?, rootKey: String?) {

            // Using this method instead of addPreferencesFromXml so we can specify the root preference screen
            // This way, navigating to a new screen is as simple as calling SettingsFragment.newInstance("new_root")
            setPreferencesFromResource(R.xml.prefs, rootKey)
        }

        companion object {

            fun newInstance(rootKey: String = "root") = SettingsFragment().apply {

                // When you pass a string argument with key ARG_PREFERENCE_ROOT, 
                // PreferenceFragmentCompat picks it up and supplies it as an argument to onCreatePreferences
                arguments = Bundle().apply { putString(ARG_PREFERENCE_ROOT, rootKey) }
            }
        }
    }
}

这是最终结果:

票数 5
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44467627

复制
相关文章

相似问题

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