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

Edw*_*ans 6 android

如何通过android中的代码隐藏标题栏描述了一种隐藏窗口标题栏的简单方法,但需要在调用setContentView之前完成.如果我想稍后再做怎么办?在我的情况下,我想在Web视图完成加载内容后执行此操作,我不再需要在标题栏中显示进度.

b_y*_*yng 2

这里有几个涉及完全放弃标题栏的选项:

  • 使用 来膨胀布局LayoutInflater。该布局本质上是LinearLayout或,其中包含标题栏RelativeLayout的所有组件。
  • 或者,如果这听起来太麻烦,您可以在活动的 xml 中创建一个标题栏,并将可见性设置为消失titleBarLayout.setVisibility(View.VISIBLE);,并在 Web 视图加载完成时使用

伪代码:

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.
}
Run Code Online (Sandbox Code Playgroud)

就我个人而言,我会选择这个LayoutInflater选项。但这取决于你。我相信您还可以向使用任一选项显示的标题栏添加动画,这可能是一个很好的补充。


或者之前调用这个setContentView

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
Run Code Online (Sandbox Code Playgroud)

如果不支持自定义标题栏,这将返回 false,因此您可能需要检查这一点。这是在之后调用的setContentView

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
Run Code Online (Sandbox Code Playgroud)

为 xml 文件的父布局(包含所有视图的LinearLayout或)分配一个带有.RelativeLayoutandroid:id="@+id/custom_title_layout"

现在,

LinearLayout titleBarLayout = (LinearLayout) findViewById(R.id.custom_title_layout);
Run Code Online (Sandbox Code Playgroud)

并使用以下命令切换标题栏是否存在:

titleBarLayout.setVisibility(View.GONE);  //View.VISIBLE to show
Run Code Online (Sandbox Code Playgroud)