【问题标题】:Alternatives to PreferenceFragment with android-support-v4使用 android-support-v4 替代 PreferenceFragment
【发布时间】:2012-03-20 08:17:01
【问题描述】:

当我意识到这个库不支持 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,我使用的是我在网上找到的东西。这是一个sn-p:

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 兼容性库来实现类似于偏好片段(或偏好活动)的东西?

【问题讨论】:

标签: android android-fragments


【解决方案1】:

我知道这是一个老问题,但您现在可以使用来自com.android.support:preference-v7:23.3.0PreferenceFragmentCompat

【讨论】:

  • 很棒的发现,现在这是正确答案之一。
  • 对我来说它只适用于 com.android.support:preference-v7:23.0.1。
  • 尝试从 Android Studio 添加它时,这似乎不在依赖服务器上。我需要为它添加一个存储库吗?
  • 它不包含那个类
  • 我不小心对这个答案投了赞成票,现在无法撤消它,所以我只注意到PreferenceFragmentCompat 有一些主要问题:请参阅thisthis 错误报告,对于例如,many more here。我最终改用了已弃用的PreferenceActivity叹息
【解决方案2】:

更新 - 2015 年 6 月 11 日

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


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

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

您可以保持一切,包括您的片段交易。导入PreferenceFragment类时,确保正确的导入头是user。

import android.support.v4.preference.PreferenceFragment;

而不是

import android.preference.PreferenceFragment;

【讨论】:

  • 那个 github 代码似乎有严重的问题...我不能使用 RingtonePreference 并且 OnSharedPreferenceChangeListener 似乎没有任何变化!
  • 我认为使用这个库是个好主意。但我意识到它并没有我想象的那么好。试图覆盖onPrepareOptionMenu 方法向我显示了一个奇怪的签名(返回void),不同于我们都知道的默认签名(返回boolean)!!!一个严重的问题。
  • 投了赞成票,只要您来马德里,您就有权享用免费晚餐……这个问题快把我逼疯了!
  • @mansoulx onPrepareOptionsMenu 在片段上总是有一个 void 返回类型(不像活动,它是布尔值),所以这个签名完全符合预期。
  • 库的问题可能应该在 github 项目上报告。
【解决方案3】:

重要更新:v7 support librarylatest revision 现在有一个原生PreferenceFragmentCompat

我正在使用this library,它在mavenCentral 中有一个AAR,因此如果您使用Gradle,您可以轻松地包含它。

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

【讨论】:

  • 您能分享一下您是如何导入和扩展支持片段的吗?谢谢
  • @Steve 当然!我做了a Gist
  • @mattblang 非常感谢!
  • @mattblang 非常感谢...您的帮助真的节省了我很多时间
  • Exception java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' 不是 ListView 类
【解决方案4】:

你可以使用我自己的PreferenceFragment

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

【讨论】:

    【解决方案5】:

    Preferences Support Library: Preference Fragments for API 7+, no matter the Activity

    一个简单的实现将包括一个 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
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

      • 我不能使用 PreferenceActivity,因为我的应用程序的其余部分由一个 tabwidget 组成。偏好片段应该显示在选项卡小部件上方。我打算让它成为一个活动,而不是它会占据整个屏幕。
      • 然后使用一些自定义列表片段
      【解决方案7】:

      您可以使用第三方库(例如 UnifiedPreferences)为所有 Android 版本使用一个 API。

      【讨论】:

        【解决方案8】:

        您可以改为从 PreferenceActivity 扩展,如果您希望有一个 ActionBar,您可以使用 Toolbar 添加它,因为它位于用于首选项的 ListView 之上。

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

        如果您愿意,可以查看我的解决方案here

        【讨论】:

          【解决方案9】:

          正如 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。 但是,您可能对主题有疑问。如果是这种情况,您可以参考这篇文章来解决它:

          Solution to make the lib work on API 7+ while keeping material theme on API 14+

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-05
            • 1970-01-01
            • 2013-08-18
            • 1970-01-01
            • 2016-05-30
            • 2015-08-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多