Applying custom theme to ActionBar in PreferenceFr

2019-04-11 06:27发布

I am trying to apply custom theme to ActionBar in PreferenceFragment. Following is the scenerio:

My class extends PreferenceFragment in which I am adding preferences from resouces as:

addPreferenceFromResource (R.xml.myPrefs);

Also, I have written:

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

Now I want to apply my own theme that I defined in styles.xml to this action bar. Currently, the height of action bar is smaller that the tab view due to which the view is getting cropped.

I tried searching but couldn't find a solution to my problem.

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

From 1. and 2. links above, I learnt that:

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);

But this also didn't work.

Any help is highly appreciated.

1条回答
Summer. ? 凉城
2楼-- · 2019-04-11 06:58

Finally found solutions to my problem. Seems like there was a simple solution while I tangled myself into complex things.

I just added the following line to the onCreate () of my PreferenceFragment class... and everything became perfect!

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

Hope it helps someone who might face the same problem! :)

查看更多
登录 后发表回答