【问题标题】:Applying custom theme to ActionBar in PreferenceFragment将自定义主题应用于 PreferenceFragment 中的 ActionBar
【发布时间】:2013-09-16 09:49:26
【问题描述】:

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

我的课程扩展了PreferenceFragment,我在其中添加了来自资源的首选项:

addPreferenceFromResource (R.xml.myPrefs);

另外,我写过:

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

现在我想将我在styles.xml 中定义的主题应用到此操作栏。目前,action bar 的高度小于导致视图被裁剪的选项卡视图。

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

  1. Set theme for a Fragment
  2. https://groups.google.com/forum/#!topic/android-developers/GX_gOAN2nmM

从上面的 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);

但这也没有用。

非常感谢任何帮助。

【问题讨论】:

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


    【解决方案1】:

    终于找到了解决我问题的方法。当我陷入复杂的事情中时,似乎有一个简单的解决方案。

    我刚刚在我的PreferenceFragment 类的onCreate () 中添加了以下行......一切都变得完美了!

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

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

    【讨论】:

    • 这不会覆盖整个活动的主题吗?它对我有用。 IE 当我更改 Fragments 时主题不同,我将不得不再次将其更改回来。
    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多