如何在创建窗口后隐藏Android标题栏?

6

如何通过代码在Android中隐藏标题栏描述了一种简单的方法来隐藏窗口标题栏,但需要在调用setContentView之前完成。如果我想稍后再做呢?在我的情况下,我希望在Web视图加载完内容并且不再需要在标题栏中显示进度时进行操作。

5个回答

2

这里有两个选项,可以完全放弃标题栏:

  • 使用 LayoutInflater 填充布局。此布局将基本上是包含所有组成部分的 LinearLayoutRelativeLayout,用于您的标题栏
  • 或者如果听起来太麻烦了,您可以在活动的 xml 中创建一个标题栏,并将可见性设置为gone,当 web 视图加载完成时,使用 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);

如果不支持自定义标题栏,它会返回 false,因此您可能需要检查一下。这在 setContentView 之后调用:
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);

给xml文件的父布局(包含所有视图的LinearLayoutRelativeLayout)分配一个id,使用android:id="@+id/custom_title_layout"

现在,

LinearLayout titleBarLayout = (LinearLayout) findViewById(R.id.custom_title_layout);

使用以下方法切换标题栏的显示与隐藏:

titleBarLayout.setVisibility(View.GONE);  //View.VISIBLE to show

我想使用标准标题栏,但稍后将其移除。 - Edwin Evans
我仍然建议使用其他方法。一个视图突然出现或消失,没有任何动画效果看起来相当无聊。 - b_yng
接受这个建议听起来很有希望,虽然我还没有验证。我本来希望找到更简单的解决方案,但现在已经切换到了另一个不需要隐藏标题栏的用户界面。 - Edwin Evans

2

如果您正在使用API 11及以上版本

ActionBar actionBar = getActionBar();
actionBar.hide(); // slides out
actionBar.show(); // slides in

1

4
根据问题指定的内容,这不是在窗口创建后发生的。 - Edwin Evans
为什么人们在提交答案之前从不仔细阅读问题? - JHHoang

0

这对我有用

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

    });

-1

在 XML 中使用

<activity android:name=".ActivityName" 
      android:theme="@android:style/Theme.NoTitleBar">

4
根据问题指定,这不是在窗口创建后。 - Edwin Evans

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接