在PreferenceFragment中将自定义主题应用于ActionBar

时间:2013-09-16 09:49:27

标签: android android-fragments android-actionbar android-preferences

我正在尝试将自定义主题应用于ActionBar中的PreferenceFragment。以下是场景:

我的课程扩展PreferenceFragment我在其中添加来自资源的偏好:

addPreferenceFromResource (R.xml.myPrefs);

另外,我写了:

ActionBar a = getActivity ().getActionBar ();
bar.setNavigationMode (ActionBar.NAVIGATION_MODE_TABS);

现在我想将我在styles.xml中定义的主题应用于此操作栏。目前,操作栏的高度小于视图被裁剪的标签视图。

我尝试过搜索,但无法找到解决问题的方法。

  1. Set theme for a Fragment
  2. https://groups.google.com/forum/#!topic/android-developers/GX_gOAN2nmM
  3. 从上面的1.和2.链接,我了解到:

    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(),R.style.yourCustomTheme);
    
    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    
    // inflate the layout using the cloned inflater, not default inflater
    return localInflater.inflate(R.layout.yourLayout, container, false);
    

    但这也行不通。

    非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

终于找到了我的问题的解决方案。似乎有一个简单的解决方案,当我把自己纠缠在复杂的事情中时。

我刚刚将以下一行添加到我的onCreate ()课程的PreferenceFragment中......一切都变得完美了!

getActivity ().setTheme (R.style.myCustomTheme);

希望它可以帮助可能面临同样问题的人! :)

相关问题