Android Studio - 移除操作栏

29

我想要移除/禁用 ActionBar。我尝试在 Manifest 文件中加入以下内容:

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

它不起作用。它不能移除ActionBar。 请帮帮我。 谢谢。


1
尝试在您的应用程序主题定义中添加 <item name="android:windowContentOverlay">@null</item> - Lal
http://prntscr.com/42mpop - iYonatan
我的minSdkVersion是7。 - iYonatan
尝试用你的方式来实现这个代码... <style name="NoActionBar" parent="Theme.AppCompat.Light"> <item name="android:windowNoTitle">true</item> </style> 并且在你的<activity>中添加 android:theme="@style/NoActionBar" - Lal
这里还有一个答案:https://dev59.com/IWoy5IYBdhLWcg3wlvCX - Charleston
显示剩余9条评论
12个回答

41

我也遇到了这个问题。我去了styles.xml并进行了更改。

parent="Theme.AppCompat.Light.DarkActionBar" 

parent="Theme.AppCompat.Light.NoActionBar"

这使我从所有的活动中都被移除了。


工作正常。谢谢!<3 - PlayHardGoPro
它对我也起作用了。所以在我看来,这就是解决方案。 - GSandro_Strongs

33

在你的oncreate()方法中使用这个。

getActionBar().hide();

如果您的minSdkVersion为10或更低版本,则请改用:

getSupportActionBar().hide();

6
当我这样做时,该应用程序会在启动时崩溃。 - PlayHardGoPro
@PlayHardGoPro 我也遇到了这个问题,但是像下面展示的更改主题对我也有效。可能自那时起有一些变化。 - Matthew

15

你可以试试这个。它对我有用。

将其添加到style.xml中。

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="windowActionBar">false</item>
      <item name="windowNoTitle">true</item>
      <item name="android:windowFullscreen">true</item>
</style>

并使用manifest.xml中的样式。

android:theme="@style/AppTheme.NoActionBar"

6

对于Android Studio,您可以进入styles.xml文件并进行更改。

parent="Theme.AppCompat.Light.DarkActionBar"

为了

parent="Theme.AppCompat.Light.NoActionBar" 

在Eclipse中,可以在清单文件中更改此设置。
  <activity
        android:name="com.ExcellenceTech.webview.MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >

5
getActionBar().hide();

我认为这应该可以工作。


3

您可以通过不同的方式隐藏操作栏。

1.通过清单xml文件(非Java编程)隐藏操作栏,则最简单的方法是在Activity标记中添加以下属性:

 android:theme="@style/Theme.NoActionBar"

如果创建了自定义主题,则方便创建另一种扩展自定义主题的样式。
 <item name="android:windowActionBar">false</item>
 <item name="android:windowNoTitle">true</item>

或者
ActionBar actionBar = getActionBar(); OR getSupportActionBar();
actionBar.hide();

getSupportActionBar().hide(); 
  or 
getActionBar().hide();

2
在AndroidManifest.xml和activity部分中放置以下代码。
android:theme="@style/Theme.AppCompat.Light.NoActionBar"

1
getActionBar().hide();

或者

getSupportActionBar().hide();

但是这个变化可能不会在xml设计中显示出来。

1

在styles.xml中永久隐藏Action Bar 如果您希望在整个应用程序(包括活动、片段)中隐藏操作栏,则可以简单地导航到res> values> styles.xml,并按照下面所述更改您的基本应用程序主题。

 <resources> 
 <!- Base application theme. →
 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
 <!- Customize your theme here. →
 <item name="colorPrimary">@color/colorPrimary</item>
 <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 <item name="colorAccent">@color/colorAccent</item>
 </style>
</resources>

隐藏特定活动的操作栏:

在onCreate()方法中使用此方法

Kotlin:

supportActionBar?.hide()

Java:

if (getSupportActionBar() != null) {
getSupportActionBar().hide();

}


0

您可以使用以下方式使用hideUi()函数,并在onCreate()方法中调用它:

private void hideUI() {
    // Hide UI first
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.hide();
    }
    View mContentView = (findViewById(R.id.mainLayout));
// Delayed removal of status and navigation bar
    if (Build.VERSION.SDK_INT >= 30) {
        mContentView.getWindowInsetsController().hide(
                WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
    } else {
        // Note that some of these constants are new as of API 16 (Jelly Bean)
        // and API 19 (KitKat). It is safe to use them, as they are inlined
        // at compile-time and do nothing on earlier devices.
        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }
}

setContentView()之后,在onCreate()中调用hideUI()函数:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (getSupportActionBar() != null) {
        getSupportActionBar().hide();
    }
    hideUI();
   // ......
}

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