【问题标题】:How to hide the title bar through code in android如何通过android中的代码隐藏标题栏
【发布时间】:2011-07-29 11:23:49
【问题描述】:

我想使用我的一些活动的代码隐藏标题栏。

我使用了以下代码

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                          WindowManager.LayoutParams.FLAG_FULLSCREEN);

第二行是全屏显示,但它显示应用程序标题。假设我的启动画面我想隐藏我的标题。第一行代码使我的应用程序崩溃。如果我们可以使用代码做到这一点,请帮助我。

谢谢。

【问题讨论】:

    标签: android android-layout android-manifest


    【解决方案1】:
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    你应该在 setContentView() 方法之前调用它,你这样做了吗?

    您始终可以通过在您的活动中添加android:theme="@android:style/Theme.NoTitleBar" 在您的清单中做到这一点

    【讨论】:

    • 标记为回答,请:)
    • 这行得通,但即使我在 setContentView() 之前调用它,标题也会显示一秒钟,然后活动会全屏显示。有人注意到这一点并知道解决方案吗??
    【解决方案2】:

    隐藏标题栏和状态栏:

    try
       {((View)act.findViewById(android.R.id.title).getParent()).setVisibility(View.GONE);
       }
    catch (Exception e) {}
    act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    act.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    view.requestLayout();
    

    显示标题栏和状态栏:

    try
       {((View)act.findViewById(android.R.id.title).getParent()).setVisibility(View.VISIBLE);
       }
    catch (Exception e) {}
    act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    act.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    view.requestLayout();
    

    【讨论】:

    • 请问,你到底在什么视图上调用requestLayout?此外,您的答案的第一行(假设隐藏标题的那个)使我的应用程序崩溃:s
    • 是的,我太困惑了。
    【解决方案3】:

    如果您使用的是 API 11 及更高版本

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

    【讨论】:

    • 如果11 想显示标题栏
    【解决方案4】:

    对于删除应用程序标题栏然后添加这一行

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

    之前

    【讨论】:

      【解决方案5】:

      确保在这两行之后调用 setContentView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多