本文介绍了如何使用V7 / V14 preference支持库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加上并购的释放,也有新的支持库。他们中的一个,这似乎是非常有用的是 V7 preference支持库。

Together with the M release, there are new support libraries. One of them that seems to be very useful is the v7 Preference Support library.

这似乎并不具有 preferenceActivity 或类似的东西,我们如何将其集成到我们的应用程序?

It does not seem to have PreferenceActivity or something similar, how do we integrate it to our app?

推荐答案

您必须扩展 AppCompatActivity ,这是需要的片段,并包括<一个子类href="https://developer.android.com/reference/android/support/v7/$p$pference/$p$pferenceFragmentCompat.html"><$c$c>$p$pferenceFragmentCompat.摘要片段需要覆盖一个方法,你应该把你的preference通胀的逻辑。而在去年,你的活动主题,需要指定一个 preferenceTheme 属性。

You have to extend AppCompatActivity, which is required for fragment, and include a subclass of PreferenceFragmentCompat. The abstract fragment requires to override one method, in which you should place your preference inflation logic. And last, your activity theme needs to specify a preferenceTheme attribute.

阅读公告这里。随着preference-V7库,您可以替换 preferenceFragment (API 11+)以 preferenceFragmentCompat 子和开关preference (API 14+)与<一个href="https://developer.android.com/reference/android/support/v7/$p$pference/Switch$p$pferenceCompat.html"><$c$c>Switch$p$pferenceCompat从API 7的设置屏幕的工作。

Read the announcement here. With preference-v7 library you can replace PreferenceFragment (API 11+) with PreferenceFragmentCompat subclass, and SwitchPreference (API 14+) with SwitchPreferenceCompat and have your settings screen work from API 7.

下面是我如何做它的工作:

Below is how I made it work:

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
    }
}

布局/ activity_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" >
    <fragment
        android:name=".SettingsFragment"
        android:tag=".SettingsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

SettingsFragment.java

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preferences);
    }
}

XML / preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <android.support.v7.preference.PreferenceCategory
        ...>

        <android.support.v7.preference.ListPreference
            ... />

        <android.support.v7.preference.SwitchPreferenceCompat
            ... />

        ...

    </android.support.v7.preference.PreferenceCategory>

    ...

</android.support.v7.preference.PreferenceScreen>

值/ styles.xml

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

preference-V7默认主题

<style name="PreferenceThemeOverlay">
    <item name="preferenceScreenStyle">@style/Preference.PreferenceScreen</item>
    <item name="preferenceFragmentStyle">@style/PreferenceFragment</item>
    <item name="preferenceCategoryStyle">@style/Preference.Category</item>
    <item name="preferenceStyle">@style/Preference</item>
    <item name="preferenceInformationStyle">@style/Preference.Information</item>
    <item name="checkBoxPreferenceStyle">@style/Preference.CheckBoxPreference</item>
    <item name="switchPreferenceCompatStyle">@style/Preference.SwitchPreferenceCompat</item>
    <item name="dialogPreferenceStyle">@style/Preference.DialogPreference</item>
    <item name="editTextPreferenceStyle">@style/Preference.DialogPreference.EditTextPreference</item>
    <item name="preferenceFragmentListStyle">@style/PreferenceFragmentList</item>
</style>

这篇关于如何使用V7 / V14 preference支持库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:30