安卓如何移除操作栏

9

我试图禁用一些活动的操作栏,但无论我尝试什么,都似乎无法移除它。

在我的 styles.xml 文件中,我有以下代码:

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

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

在我的manifest.xml文件中,我有:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".NewActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
</application>

我的活动布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_new"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

我的app_bar_new布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="net.binarysea.sensorload.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_new" />

</android.support.design.widget.CoordinatorLayout>

我希望在除了NewActivity以外的活动上显示操作栏。我使用意图调用NewActivity,此时我认为它会加载AppTheme.NoActionBar样式,但是操作栏仍然出现在屏幕上。
如果这有所区别,我正在使用Android Studio中的导航抽屉式活动。
编辑:我尝试了所有建议,添加了Theme.AppCompat.Light.NoActionBar,但这只使我的操作栏变白,实际上并没有删除它。我在新的Android Studio项目中测试了这一点,创建了一个导航抽屉式活动,并将主题设置添加到我的styles.xml中。我仍然遇到同样的问题。

2
android:theme="@style/Theme.AppCompat.Light.NoActionBar" 将其放置在适当的活动中,可以消除我的应用程序上的它。 - Script Kitty
发布你的activity_layout.xml。 - Silambarasan Poonguti
我已经添加了我的布局文件。是的,那就是我想要的(除了汉堡包和设置图标)。 - Simon
6个回答

12
请尝试以下方法...
只需为您的样式添加一个父级标签。 Styles.xml
 <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
   <item name="windowActionBar">false</item>
   <item name="windowNoTitle">true</item>
 </style>

AndroidManifest.xml

 .......

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

     <activity
        android:name=".NewActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
      </activity>
   </application>
    ........

2
仅添加Theme.AppCompat.Light.NoActionBar只会使操作栏变为白色,而不是将其移除。请查看我的编辑。 - Simon

3
尝试将以下内容添加到您的清单中:android:theme="@style/Theme.AppCompat.Light.NoActionBar"。这将使用无操作栏的亮色AppCompat主题。请注意保留HTML标记,但不要写解释。

2
仅添加Theme.AppCompat.Light.NoActionBar只会使操作栏变为白色,而不是将其移除。请查看我的编辑。 - Simon

3

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
    getSupportActionBar().hide(); // hide the title bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
    setContentView(R.layout.activity_main);
}}

2

0
在styles.xml中使用如下:
<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->

</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/drawer_bg</item>

</style>

为工具栏创建单独的布局。

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

</android.support.v7.widget.Toolbar>

在任何活动中,使用以下代码访问此工具栏。
Toolbar toolbar; //declaration

在setContentView()之后的onCreate()中使用这个。
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

你使用的清单文件和样式文件将删除动作栏,要用工具栏替换动作栏,请使用我的代码。它会在你的活动中添加工具栏。 - Mohd Asif Ahmed
1
仅添加Theme.AppCompat.Light.NoActionBar只会使操作栏变为白色,而不是将其移除。请查看我的编辑。 - Simon
在样式中将windowNoTitle设置为false - Mohd Asif Ahmed

0
你可以在NewActivity的onCreate()方法中尝试这样做,以隐藏特定活动的操作栏。
ActionBar actionBar = getSupportActionBar();
actionBar.hide();

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