简体   繁体   English

Android:如何在某些活动中隐藏 ActionBar

[英]Android: how to hide ActionBar on certain activities

I've developed a simple demo application with a splash screen a map and some regular screens.我开发了一个简单的演示应用程序,其中包含初始屏幕 a map 和一些常规屏幕。

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 上测试它时,操作栏也出现在初始屏幕和 map 屏幕中。

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 继承,所以这怎么可能,我怎样才能让它 go 消失?

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);

    }

}

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

getSupportActionBar().hide(); 

Apply the following in your Theme for the Activity in AndroidManifest.xml :在您的主题中为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.那应该可以解决问题。

1.Go to your manifest.xml file. 1.转到您的manifest.xml文件。

2. Find the activity tag for which you want to hide your ActionBar and then add , 2. Find要隐藏 ActionBar 的activity tag ,然后add ,

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

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

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> 

The ActionBar usually exists along Fragments so from the Activity you can hide it ActionBar通常存在于Fragments因此您可以从Activity中隐藏它

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

and from the Fragment you can do itFragment你可以做到

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

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.希望这有帮助。

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>

To hide the ActionBar add this code into java file.要隐藏 ActionBar,请将此代码添加到 java 文件中。

    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();

You can use Low Profile mode See here您可以使用低调模式请参阅此处

Just search for SYSTEM_UI_FLAG_LOW_PROFILE that also dims the navigation buttons if they are present of screen.只需搜索SYSTEM_UI_FLAG_LOW_PROFILE ,如果它们出现在屏幕上,它也会使导航按钮变暗。

The above answers would help with the ActionBar thing.上面的答案将有助于 ActionBar 的事情。 To add to it, use the following code in case you are using the Splash Screen: Use this before you set the content view:要添加它,如果您使用的是启动画面,请使用以下代码:在设置内容视图之前使用此代码:

    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, ie remove the top bar where you see the network bar, etc这将使您的屏幕全屏,即删除您看到网络栏等的顶部栏

Apply the following in your Theme for the Activity in AndroidManifest.xml:在您的主题中为 AndroidManifest.xml 中的活动应用以下内容:

<activity android:name=".DashboardActivity"
        android:theme="@style/AppFullScreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Then Apply the following in your Style in style.xml然后在 style.xml 中的 Style 中应用以下内容

<style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

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)如果你有问题切换到线性布局(因为这是测试代码的内容)

From here :这里

Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar.从 Android 3.0(API 级别 11)开始,所有使用默认主题的 Activity 都有一个 ActionBar 作为应用栏。 However, app bar features have gradually been added to the native ActionBar over various Android releases.但是,在各种 Android 版本中,应用栏功能已逐渐添加到原生 ActionBar 中。 As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using .因此,本机 ActionBar 的行为取决于设备可能使用的 Android 系统版本 By contrast, the most recent features are added to the support library's version of Toolbar , and they are available on any device that can use the support library.相比之下,最新的功能被添加到支持库的Toolbar版本中,并且可以在任何可以使用支持库的设备上使用。

So one option is to disable ActionBar completely (in the app manifest.xml file) and then add Toolbar in every page that needs an ActionBar.因此,一种选择是完全禁用 ActionBar(在应用程序 manifest.xml 文件中),然后在需要 ActionBar 的每个页面中添加 Toolbar。

(follow the above link for step-by-step explanation) (按照上面的链接一步一步的解释)

For selectively hiding Actionbar in activities:有选择地隐藏活动中的操作栏:

Add the following code to onCreate (preferably on the last line of the function)将以下代码添加到onCreate(最好在函数的最后一行)

Kotlin:科特林:

supportActionBar?.hide()

Java:爪哇:

getSupportActionBar().hide();

@Override
    public void onResume() {
        super.onResume();
        getSupportActionBar().hide();
    }

    @Override
    public void onStop() {
        super.onStop();
        getSupportActionBar().show();
    }

Replace AndroidManifest.xml替换 AndroidManifest.xml

android:theme="@style/FullscreenTheme"

to

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

Add New Style in your style.xml在 style.xml 中添加新样式

<!-- Base application theme. -->
<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="LoginTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

and then add following line in your manifest然后在清单中添加以下行

<activity android:name=".Login"
        android:label="@string/app_name"
        android:theme="@style/LoginTheme"
        >

Check for padding attribute if the issue still exists after changing theme.如果更改主题后问题仍然存在,请检查填充属性。

Padding Issue creating a white space on top of fragment window / app window填充问题在片段窗口/应用程序窗口顶部创建一个空白区域

填充问题在片段窗口/应用程序窗口顶部创建一个空白区域

Once you remove the padding - top value automatically the white space is removed.删除填充 - 顶部值后,空白将自动删除。

First cheek MainActivity for activity tag,Like -活动标签的第一个脸颊 MainActivity,例如 -

public class MainActivity extends AppCompatActivity公共类 MainActivity 扩展了AppCompatActivity

Then goto menifest xml file and write-然后转到menifest xml文件并写入-

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

in activity tag在活动标签中

If you activity extends AppCompatActivity then you can add this line super.getSupportActionBar().hide();如果您的活动扩展了AppCompatActivity,那么您可以添加这一行super.getSupportActionBar().hide(); before setContentView()setContentView()之前

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

这对我有用

I recommend this article that explains several ways to do it.我推荐这篇文章,它解释了几种方法。 Although it is in Spanish, you can easily translate or intuit its content through the code, I hope it will help you!虽然是西班牙语,但您可以通过代码轻松翻译或直观了解其内容,希望对您有所帮助! https://masprogramar.blogspot.com/2021/11/android-ocultar-la-actionbar.html https://masprogramar.blogspot.com/2021/11/android-ocultar-la-actionbar.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM