使用android-support-v4替代PreferenceFragment

Cod*_*ate 90 android android-fragments

我突然停止了我的应用程序的开发,因为我意识到这个库中不支持PreferenceFragments.新手Android开发人员可以使用任何替代方案来克服这个障碍吗?

这是我现在的主要窗口

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@+android:id/realtabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>

                <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            />

</TabHost>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

对于我的TabActivity,我使用的是我在网上找到的东西.这是一个片段:

public class TabControlActivity extends FragmentActivity implements TabHost.OnTabChangeListener 
{
public static final int INSERT_ID = Menu.FIRST;
public static TabControlActivity thisCtx;
private TabHost mTabHost;
private HashMap mapTabInfo = new HashMap();
private TabInfo mLastTab = null;

private class TabInfo {
     private String tag;
     private Class clss;
     private Bundle args;
     private Fragment fragment;
     TabInfo(String tag, Class clazz, Bundle args) {
         this.tag = tag;
         this.clss = clazz;
         this.args = args;
     }

}

class TabFactory implements TabContentFactory 
{

    private final Context mContext;

    /**
     * @param context
     */
    public TabFactory(Context context) {
        mContext = context;
    }

    /** (non-Javadoc)
     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
     */
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }

}

...*snip*...
Run Code Online (Sandbox Code Playgroud)

有没有使用android-support-v4兼容库实现类似于preferencefragment(或preferenceActivity)的东西?

Luc*_*ius 128

我知道这是一个老问题,但你现在可以使用PreferenceFragmentCompatcom.android.support:preference-v7:23.3.0

  • 很好的发现,这是现在正确的答案之一. (4认同)

Sub*_*ian 86

更新 - 2015年11月11日

Support-v7库现在包括PreferenceFragmentCompat.所以使用它会更好.


将以下项目作为库项目添加到应用程序中.

https://github.com/kolavar/android-support-v4-preferencefragment

您可以保留包括片段事务在内的所有内容.导入PreferenceFragment类时,请确保正确的导入标头是用户.

import android.support.v4.preference.PreferenceFragment;
Run Code Online (Sandbox Code Playgroud)

代替

import android.preference.PreferenceFragment;
Run Code Online (Sandbox Code Playgroud)

  • upvoted,每当你来到马德里,你都有权享受免费晚餐...这个问题让我发疯了! (9认同)
  • @mansoulx onPrepareOptionsMenu在片段上总是有一个void返回类型(与Activities不同,它是boolean),因此这个签名完全符合预期. (7认同)
  • 那个github代码似乎有严重的问题......我不能使用`RingtonePreference`而且`OnSharedPreferenceChangeListener`似乎没有得到任何改变! (4认同)
  • 应该在github项目中报告库的问题. (3认同)
  • 我认为使用这个库是个好主意.但我意识到它并不像我想象的那么好.试图覆盖`onPrepareOptionMenu`方法向我展示了一个奇怪的签名(返回`void`),与我们都知道的默认签名(返回`boolean`)不同!一个严重的问题. (2认同)
  • 如果有人像我一样,这是他们正在寻找的依赖:编译'com.android.support:preference-v7:23.4.0' (2认同)

the*_*ang 16

重要更新:最新版本v7 support library现在有一个本地PreferenceFragmentCompat.

我正在使用这个库,它有一个AARin,mavenCentral所以你可以很容易地包含它,如果你正在使用Gradle.

compile 'com.github.machinarius:preferencefragment:0.1.1'


Bla*_*der 10

你可以用我自己的PreferenceFragment.

这很简单,到目前为止我没有任何问题.我认为你只能在一个Activity中一次显示其中一个,这应该没问题.


li2*_*li2 9

首选项支持库:API 7+的首选项片段,无论活动如何

一个简单的实现包括PreferenceFragmentCompat,例如:

public class PreferencesFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences);
    }
}
Run Code Online (Sandbox Code Playgroud)

您还需要在主题中设置preferenceTheme:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
  <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并在依赖项中添加它:http: //developer.android.com/tools/support-library/features.html

com.android.support:preference-v14:23.1.0
Run Code Online (Sandbox Code Playgroud)


Sni*_*las 7

您可能会使用真实的活动并使用片段,但我不认为这是一个不错的选择.您应该使用简单的PreferenceActivity并等待retro compat libs的改进.

  • 然后使用一些自定义列表片段 (2认同)