Java Android:如何隐藏某些活动的 ActionBar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19545370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:15:09  来源:igfitidea点击:

Android: how to hide ActionBar on certain activities

javaandroidandroid-actionbar

提问by Tomer

I've developed a simple demo application with a splash screen a map and some regular screens.

我开发了一个简单的演示应用程序,带有启动画面、地图和一些常规屏幕。

I have an action bar at the top that contains a logo. It all looks fine on my phone (Galaxy s1 I9000 V2.3) but when i test it on Galaxy s2 v4 the action bar appears also in the splash screen and in the map screen.

我在顶部有一个包含徽标的操作栏。在我的手机(Galaxy s1 I9000 V2.3)上看起来一切正常,但是当我在 Galaxy s2 v4 上测试时,操作栏也会出现在启动画面和地图屏幕中。

The spalsh and map activity are not even inheriting from ActionBarActivity so how is that possible and how can i make it go away?

spalsh 和 map 活动甚至不是从 ActionBarActivity 继承的,所以这怎么可能,我怎样才能让它消失?

Manifest:

显现:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.AppCompat.Light" >
        <activity
            android:name=".HomeActivity"
            android:icon="@drawable/android_logo"
            android:label=""
            android:logo="@drawable/android_logo" >

            <!--
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            -->
        </activity>
        <activity
            android:name=".MapActivity"
            android:label="" >
        </activity>
        <activity
            android:name=".PackageActivity"
            android:icon="@drawable/android_logo"
            android:label=""
            android:logo="@drawable/android_logo" >
        </activity>
        <activity
            android:name=".SplashActivity"
            android:label="" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

MapActivity definition (it's a long one so i included just the definition):

MapActivity 定义(它很长,所以我只包含了定义):

public class MapActivity extends FragmentActivity implements LocationListener

Splash Activity:

飞溅活动:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

public class SplashActivity extends Activity{

    private static final long SPLASH_DISPLAY_LENGTH = 2000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                Intent mainIntent = new Intent(SplashActivity.this,HomeActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);

    }

}

采纳答案by Si8

Apply the following in your Theme for the Activity in AndroidManifest.xml:

在 Activity 的主题中应用以下内容AndroidManifest.xml

<activity android:name=".Activity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

That should do the trick.

这应该够了吧。

回答by TMH

You can use Low Profile mode See here

您可以使用低调模式 请参阅此处

Just search for SYSTEM_UI_FLAG_LOW_PROFILEthat also dims the navigation buttons if they are present of screen.

SYSTEM_UI_FLAG_LOW_PROFILE如果它们出现在屏幕上,只需搜索它也会使导航按钮变暗。

回答by erluxman

As you are asking about how to hidein a certain activity, this is what you need :

当您询问如何隐藏某个活动中时,这就是您所需要的:

getSupportActionBar().hide(); 

回答by sandalone

The ActionBarusually exists along Fragmentsso from the Activityyou can hide it

ActionBar通常沿着存在Fragments所以从Activity您可以将其隐藏

getActionBar().hide();
getActionBar().show();

and from the Fragmentyou can do it

Fragment你可以做到

getActivity().getActionBar().hide();
getActivity().getActionBar().show();

回答by Prateek Joshi

1.Go to your manifest.xmlfile.

1.转到您的manifest.xml文件。

2.Findthe activity tagfor which you want to hide your ActionBar and then add,

2.Findactivity tag您要隐藏你的动作条,然后add

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

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

           <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            >

回答by joshgoldeneagle

If you were using Theme.AppCompat.Light, a better equivalent would be Theme.AppCompat.Light.NoActionBar.

如果您使用的是 Theme.AppCompat.Light,则更好的等效项是 Theme.AppCompat.Light.NoActionBar。

I found that using Theme.AppCompat.NoTitleBar caused my button text to be invisible so I am using Theme.AppCompat.Light.NoActionBar.

我发现使用 Theme.AppCompat.NoTitleBar 导致我的按钮文本不可见,所以我使用 Theme.AppCompat.Light.NoActionBar。

<activity android:name=".Activity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.AppCompat.Light.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

回答by reza.cse08

If you want to get full screen without actionBar and Title.

如果你想在没有 actionBar 和 Title 的情况下获得全屏。

Add it in style.xml

在 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>

and use the style at activity of manifest.xml.

并在 manifest.xml 的活动中使用样式。

<activity ....
      android:theme="@style/AppTheme.NoActionBar" > ......
</activity> 

回答by BlueLeaf

The above answers would help with the ActionBar thing. To add to it, use the following code in case you are using the Splash Screen: Use this before you set the content view:

上面的答案将有助于 ActionBar 的事情。要添加它,如果您使用的是启动画面,请使用以下代码:在设置内容视图之前使用此代码:

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

Just to clarify, here's how you do it:

只是为了澄清,这里是你如何做到这一点:

    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);??????? ?
    setContentView(R.layout.activity_main);

This would make your screen a full screen, i.e remove the top bar where you see the network bar, etc

这将使您的屏幕全屏,即删除您看到网络栏等的顶部栏

回答by DragonFire

This works in API 27

这适用于 API 27

In the styles.xml replace code to the following....

在styles.xml 中将代码替换为以下....

<resources>
    <!-- No Action Bar -->
    <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>

Then in the files (eg. activity_list.xml) in which you do want to have a toolbar put the following code.

然后在您确实希望有工具栏的文件(例如activity_list.xml)中放置以下代码。

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/colorPrimary"/>

If you have problems switch to linear layout (because that is what this code is tested on)

如果你有问题切换到线性布局(因为这是测试代码的内容)

回答by Kyo Huu

This works for me! Put this in your Activity in manifests

这对我有用!将此放在清单中的活动中

<activity
   // ...
   android:theme="@style/Theme.AppCompat.Light.NoActionBar"
   // ...
   </activity>

You can also make your custom theme in styles.xml like this:

您还可以在styles.xml 中制作自定义主题,如下所示:

<style name="Theme.MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name = "android:windowActionBar">false</item>
        <item name = "android:windowNoTitle">true</item>
        // more custom...
    </style>

Hope this help.

希望这有帮助。