android-support-v4替代PreferenceFragment


90

当我意识到该库不支持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>

对于我的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*...

是否可以使用android-support-v4兼容性库来实现类似于preferencefragment(或preferenceActivity)的东西?



在我“需要”这些首选项显示在tabhost中的情况下,这并不是真正的重复。见我的编辑
CodePrimate

请参阅我的最新答案PreferenceFragmentCompat是最近添加到支持库的。
theblang

Answers:


86

更新-6/11/2015

Support-v7库现在包括PreferenceFragmentCompat。因此,使用它会是一个更好的主意。


将以下项目作为库项目添加到您的应用程序。

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

您可以按原样保留包括片段事务在内的所有内容。导入PreferenceFragment类时,请确保正确的导入标头是user。

import android.support.v4.preference.PreferenceFragment;

代替

import android.preference.PreferenceFragment;

4
那个github代码似乎有严重的问题...我不能使用RingtonePreferenceOnSharedPreferenceChangeListener而且似乎也没有任何改变!
2014年

2
我当时认为使用此库是个好主意。但是我意识到这并不像我想的那样好。尝试覆盖onPrepareOptionMenu方法会向我显示一个奇怪的签名(返回void),不同于我们都知道的默认签名(返回boolean)!一个严重的问题。
S.Thiongane 2014年

9
投票,只要您来马德里就可以免费享用晚餐...这个问题使我发疯!
2014年

7
@mansoulx onPrepareOptionsMenu的片段上始终有一个无效的返回类型(不同于Activity,因为它是布尔值),因此此签名完全符合预期。
Cumbayah 2014年

3
库的问题可能应该在github项目上报告。
Mathijs Segers 2015年

128

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


4
很好的发现,这是现在的正确答案之一。
Mythul

对我来说,它仅适用于com.android.support:preference-v7:23.0.1。
费利佩·莫索

1
尝试从Android Studio添加依赖项时,它似乎不在依赖服务器上。我是否需要为其添加存储库?
Brill Pappin

它不包含该课程
JJ Ab

1
我不小心upvoted这个答案,现在我不能撤销它,所以我将仅仅注意到,PreferenceFragmentCompat有一些主要问题:看到这个这个 bug报告,例如,和更多的在这里。我最终使用了不推荐使用的东西PreferenceActivity感叹
Vicky Chijwani,

16

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

我正在使用这个库,它具有AARin,mavenCentral因此如果您正在使用,可以轻松地将其包括在内Gradle

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


您能否分享如何导入和扩展支持片段?谢谢
史蒂文

1
@Steve当然!我做了一个要点
theblang 2014年

@mattblang非常感谢!
史蒂文

@mattblang非常感谢...您的帮助确实节省了我很多时间
Asad Ullah Rafiq

异常java.lang.RuntimeException:内容具有ID属性为“ android.R.id.list”的视图,该视图不是ListView类
Abdul Rahman

10

你可以用我自己的PreferenceFragment

它很简单,到目前为止我还没有问题。我认为您一次只能在一个活动中显示其中一个,这应该没问题。


9

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

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

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

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

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

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

com.android.support:preference-v14:23.1.0

7

您可能会使用真实的活动并使用片段,但是我认为这不是一个好选择。您应该使用简单的PreferenceActivity并等待复古兼容库中的改进。


1
我无法使用PreferenceActivity,因为我的应用程序其余部分都包含一个tabwidget。偏好片段应该显示在选项卡上方。我本来是要使其活动,但它会占用整个屏幕。
CodePrimate 2012年

2
然后使用一些自定义列表片段
zapl 2012年


0

您可以改为从PreferenceActivity扩展,如果希望拥有ActionBar,则可以使用工具栏添加它到用于首选项的ListView上方。

这可以通过使用包含工具栏的垂直LinearLayout和带有android:id =“ @ android:id / list”的ListView来完成。

如果您愿意,可以在此处查看我的解决方案。


0

正如Lucius所说的,您可以使用PreferenceFragmentCompat

public class MyPreferenceFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle bundle, String s) {               
            PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
            addPreferencesFromResource(R.xml.preferences);
       }
}

您必须在gradle中包括其依赖项:

dependencies {
...
    compile 'com.android.support:preference-v7:23.1.X' (wherX = 0,1,...)
...
}

这样,您还可以使用android.support.v4.app.FragmentTransaction和PrefernceFragment的FragmentTransaction。但是,您可能在主题方面遇到问题。如果是这种情况,您可以考虑此问题来解决:

使lib在API 7+上运行同时又在API 14+上保留实质主题的解决方案

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.