Adding title back to Android window when using PhoneGap
I'm building an app using PhoneGap, which calls
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
in the parent onCreate (DroidGap). But, I want to add the title back on so I can use an ActionBar. Is there any flag/way to reset this feature back? If worst comes to worst, I can modify the entire DroidGap cl开发者_开发技巧ass, but would like to still use their version.
And in case it helps anyone to see what DroidGap does in its onCreate, here's the link: https://github.com/phonegap/phonegap-android/blob/master/framework/src/com/phonegap/DroidGap.java
ActionBar code:
super.onCreate(savedInstanceState);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR); super.init();
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // here NPE is thrown
bar.addTab(bar.newTab().setText("Some Text").setTabListener(new CustomTabListener()));
Phonegap / Cordova version 1.9 added a flag to toggle this feature:
if(!this.getBooleanProperty("showTitle", false))
{
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
Example onCreate method that enables window title:
@Override
public void onCreate(Bundle savedInstanceState) {
// enable window title for actionbar support
super.setBooleanProperty("showTitle", true);
super.onCreate(savedInstanceState);
}
Have you tested FEATURE_CUSTOM_TITLE
?
getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setTitle(getResources().getString(R.string.app_name));
or try loading Theme with title bar:
setTheme(android.R.style.choose some of the themes listed or create your own);
So, I ended up not being able to solve this without modifying the original code. What I ended up doing was copying the DroidGap class into my own class (source code linked in the post) and commented out the following line:
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Then, I extended my normal activity with my modified version of DroidGap, not PhoneGap's version.
Then, in my activity's onCreate, I have something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.addTab(bar.newTab().setText("Home").setTabListener(new CustomTabListener()));
bar.addTab(bar.newTab().setText("Something").setTabListener(new CustomTabListener()));
loadUrl("file:///android_asset/www/index.html");
}
Without the requestFeature, you'll get null
from the getActionBar()
.
Hope it helps someone else!
Check the Android Manifest. I found that phonegap generates an Android manifest that specifies android:theme="@android:style/Theme.Black.NoTitleBar". Changing this theme allowed me to control the title display from code.
精彩评论