如何通过代码在Android中隐藏标题栏描述了一种简单的方法来隐藏窗口标题栏,但需要在调用setContentView之前完成。如果我想稍后再做呢?在我的情况下,我希望在Web视图加载完内容并且不再需要在标题栏中显示进度时进行操作。
如何通过代码在Android中隐藏标题栏描述了一种简单的方法来隐藏窗口标题栏,但需要在调用setContentView之前完成。如果我想稍后再做呢?在我的情况下,我希望在Web视图加载完内容并且不再需要在标题栏中显示进度时进行操作。
这里有两个选项,可以完全放弃标题栏:
LayoutInflater
填充布局。此布局将基本上是包含所有组成部分的 LinearLayout
或 RelativeLayout
,用于您的标题栏。titleBarLayout.setVisibility(View.VISIBLE);
伪代码:
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.
}
就我个人而言,我会选择使用LayoutInflater
选项。但这取决于你。我相信你也可以使用任一选项来添加标题栏显示的动画效果,这可能是一个不错的补充。
或在setContentView
之前调用此函数:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView
之后调用:getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
给xml文件的父布局(包含所有视图的LinearLayout
或RelativeLayout
)分配一个id,使用android:id="@+id/custom_title_layout"
。
现在,
LinearLayout titleBarLayout = (LinearLayout) findViewById(R.id.custom_title_layout);
使用以下方法切换标题栏的显示与隐藏:
titleBarLayout.setVisibility(View.GONE); //View.VISIBLE to show
如果您正在使用API 11及以上版本
ActionBar actionBar = getActionBar();
actionBar.hide(); // slides out
actionBar.show(); // slides in
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
...
}
这对我有用
在onCreate
中
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
然后在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);
}
});
在 XML 中使用
<activity android:name=".ActivityName"
android:theme="@android:style/Theme.NoTitleBar">