10

当我将设备转为横向时,我希望隐藏我的标题栏。我已经看到了隐藏标题栏的 XML 选项,以及以下以编程方式执行此操作的示例:

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

但是,我正在使用configChanges参数 for orientationand screenSize,因此当我的活动面向横向时,它不会再次重新创建(我这样做是出于某些原因)。所以我不能使用上述方法,因为这些调用需要在之前进行setContentView()

那么有什么想法吗?谢谢你。

4

5 回答 5

19

正在寻找答案,最终来到这里,把这些碎片放在一起,它们就在这里:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
于 2015-05-07T00:44:28.497 回答
7

假设您已经在清单中定义configChanges了,那么您可以实现您的问题覆盖方法:ActivityonConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);
     if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
          getActionBar().hide();
     } else {
          getActionBar().show();
     }
}

如果使用支持库,则必须使用getSupportActionBar()而不是。getActionBar()

于 2013-10-09T14:36:18.303 回答
3

first check your device orientation by using following code

The current configuration, as used to determine which resources to retrieve etc, as available from the Resources' Configuration object as:

getResources().getConfiguration().orientation

Then do the necessary coding related to hide title bar and notification bar.

于 2012-08-08T04:31:47.643 回答
0
@Override
public void onConfigurationChanged(Configuration config) {
     super.onConfigurationChanged(config);
     if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
          getActionBar().hide();
     } else {
          getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
          getActionBar().show();
     }
}

您可以使用我自己尝试过的这个。

于 2021-03-06T04:33:30.207 回答
-2

在“AndroidManifest.xml”的活动标签中,您可以在主题属性中指定“NoTitleBar”,以便它始终隐藏标题:

<activity
    android:name="com.my_package.MyActivity" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="navigation|orientation|screenLayout|layoutDirection">
于 2014-09-22T21:38:14.097 回答