How to hide Android title bar after window creation?
How to hide the title bar through code in android describes a simple way to hide the window title bar, but it needs to be done before calling setContentView. What if I want to do i开发者_运维知识库t later? In my case, I'd like to do it after a web view has finished loading content and I no longer need to show progress in the title bar.
Here's a couple options that involve ditching the title bar altogether:
- Inflate a layout using
LayoutInflater
. This layout will essentially be theLinearLayout
orRelativeLayout
that holds all of the components for your title bar. - or if that sounds like too much of a hastle you can create a title bar in your xml of the activity with the visibility set to gone and use
titleBarLayout.setVisibility(View.VISIBLE);
when web view is done loading
Pseudo code:
RelativeLayout activityLayout = (RelativeLayout) findViewById(R.id.my_layout);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public void onWebViewFinishLoading() {
LinearLayout myTitleBar = inflater.inflate(R.layout.my_title_bar, activityLayout, false);
//Set the view to the top of the screen
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
myTitleBar.setLayoutParams(params);
//set up buttons, listeners, etc.
}
Personally, I'd go with the LayoutInflater
option. But it's up to you. I believe you can also add animations to your title bar being displayed with either option, which could be a nice addition.
Or call this before setContentView
:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
This will return false if a custom title bar is not supported, so you may want to check for that. This is called after setContentView
:
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
Assign the parent layout of the xml file (the LinearLayout
or RelativeLayout
that holds all of the views) an id with android:id="@+id/custom_title_layout"
.
Now,
LinearLayout titleBarLayout = (LinearLayout) findViewById(R.id.custom_title_layout);
And toggle the title bar to be there or not using :
titleBarLayout.setVisibility(View.GONE); //View.VISIBLE to show
If you are using API 11 and above
ActionBar actionBar = getActionBar();
actionBar.hide(); // slides out
actionBar.show(); // slides in
If you want to hide title bar in an activity:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
...
}
Please refer to http://mgmblog.com/2008/12/08/hide-the-title-bar-in-an-android-view-by-using-the-window-class/
This worked for me
in onCreate
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
and then in WebViewClient
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
activity.setTitle("Loading...");
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundColor(Color.BLACK);
titleBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
activity.setTitle("");
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setVisibility(View.GONE);
}
});
in xml use
<activity android:name=".ActivityName"
android:theme="@android:style/Theme.NoTitleBar">
精彩评论