我使用PreferenceScreen时遇到问题,我得到了
Android.Views.InflateException:二进制XML文件行#1:错误 膨胀类android.support.v7.preference.PreferenceScreen
我的目标是构建一个设置页面,并且我想要使用旧版手机的v7版本,所以我想将一个PreferenceFragmentCompat放在一个Activity中,但我总是得到这个错误。
从NuGet我添加了对Xamarin.Android.Support.v7.Preference和Xamarin.Android.Support.v14.Preference的引用
以下是我使用的代码。
活动xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<FrameLayout
android:id="@+id/pref_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
活动代码:
public class SettingsActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Settings);
FragmentTransaction transaction = SupportFragmentManager.BeginTransaction();
transaction.Replace(Resource.Id.pref_container, new SettingsFragment()).Commit();
}
}
PreferenceScreen xml(SettingsFragment.xml):
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.PreferenceCategory
android:title="Category 1">
<android.support.v7.preference.SwitchPreferenceCompat
android:key="key1"
android:title="Switch Preference"
android:summary="Switch Summary"
android:defaultValue="true" />
<android.support.v7.preference.EditTextPreference
android:key="key2"
android:title="EditText Preference"
android:summary="EditText Summary"
android:dialogMessage="Dialog Message"
android:defaultValue="Default value" />
<android.support.v7.preference.CheckBoxPreference
android:key="key3"
android:title="CheckBox Preference"
android:summary="CheckBox Summary"
android:defaultValue="true"/>
</android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>
PreferenceScreen片段代码:
public class SettingsFragment : PreferenceFragmentCompat
{
public override void OnCreatePreferences(Bundle savedInstanceState, string rootKey)
{
AddPreferencesFromResource(Resource.Xml.SettingsFragment);
}
}
样式:
<style name="MyThemeLight.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="colorAccent">@color/primary</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>