簡體   English   中英

如何使用 Android 的 DropDownPreference?

[英]How do I use Android's DropDownPreference?

我正在嘗試使用DropDownPreference構建首選項屏幕。 最初我在我的 gradle 文件中使用以下內容compile 'com.android.support:preference-v14:25.3.1'但是當我注意到 DropDownPreference 時切換到compile 'com.android.support:preference-v7:25.3.1'包含在 v7 中,而不是 v14(我認為 v14 可能還包含 v7 中的所有內容,但我猜不是?)。 我的 XML 如下所示:

<?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 嗎? 謝謝!

編輯:添加錯誤消息:

 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

編輯:AndroidManifest.xml 中的 SettingsActivity 聲明

<activity android:name=".main.SettingsActivity"
  android:configChanges="orientation|screenSize"
  android:theme="@style/PreferenceThemeOverlay.v14.Material">
</activity>

跟進我的評論。 代碼在Kotlin順便說一句:)

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>

activity_settings.xml

<LinearLayout android:orientation="vertical">

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

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

</LinearLayout>

prefs.xml正是您所遇到的問題

Settings.kt

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) }
            }
        }
    }
}

這是最終結果:

在此處輸入圖片說明

這是 XML 中的示例

<DropDownPreference
    android:key="dropdown"
    android:title="@string/title_dropdown_preference"
    android:entries="@array/entries"
    app:useSimpleSummaryProvider="true"
    android:entryValues="@array/entry_values"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM