我尝试使用DropDownPreference构建首选项屏幕。最初我在我的gradle文件compile 'com.android.support:preference-v14:25.3.1'
中使用了以下内容,但当我注意到DropDownPreference包含在v7中而不是v14时(我认为v14也可能包含v7中的所有内容,但我猜不是?)。我的XML看起来像这样:
compile 'com.android.support:preference-v7:25.3.1'
我还尝试<?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
错误。
知道如何使用这个DropDownPreference吗?谢谢!
编辑:添加错误消息:
Error inflating class DropDownPreference
编辑:AndroidManifest.xml中的SettingsActivity声明
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
答案 0 :(得分:4)
跟进我的评论。代码在Kotlin btw:)
<强> 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) }
}
}
}
}
这是最终结果: