Java Android:如何在某些活动中隐藏ActionBar
我开发了一个简单的演示应用程序,它有一个启动屏幕、一张地图和一些常规屏幕 我在顶部有一个包含徽标的操作栏。在我的手机(Galaxy s1 I9000 V2.3)上看起来一切正常,但当我在Galaxy s2 v4上测试它时,操作栏也会出现在启动屏幕和地图屏幕上 spalsh和map活动甚至都不是从ActionBarActivity继承而来的,那么这是怎么可能的,我怎样才能让它消失呢 舱单:Java Android:如何在某些活动中隐藏ActionBar,java,android,android-actionbar,Java,Android,Android Actionbar,我开发了一个简单的演示应用程序,它有一个启动屏幕、一张地图和一些常规屏幕 我在顶部有一个包含徽标的操作栏。在我的手机(Galaxy s1 I9000 V2.3)上看起来一切正常,但当我在Galaxy s2 v4上测试它时,操作栏也会出现在启动屏幕和地图屏幕上 spalsh和map活动甚至都不是从ActionBarActivity继承而来的,那么这是怎么可能的,我怎样才能让它消失呢 舱单: <application android:allowBackup="true"
<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>
飞溅活动:
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);
}
}
在主题中为
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>
这应该可以解决问题。您可以使用低调模式
只需搜索
SYSTEM\u UI\u FLAG\u LOW\u PROFILE
,如果导航按钮出现在屏幕上,导航按钮也会变暗。当您询问如何在特定活动中隐藏时,您需要:
getSupportActionBar().hide();
ActionBar
通常沿着片段存在,因此可以从活动中隐藏它
getActionBar().hide();
getActionBar().show();
从片段
中,您可以执行此操作
getActivity().getActionBar().hide();
getActivity().getActionBar().show();
1.转到您的manifest.xml
文件
2.查找要隐藏其操作栏的活动标签
,然后添加
android:theme=“@style/theme.AppCompat.NoActionBar”
如果您使用的是Theme.AppCompat.Light,则更好的等效项是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>
我发现使用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>
如果您想在没有操作栏和标题的情况下获得全屏效果
将其添加到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>
<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>
<!-- 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>
假的
真的
真的
并在manifest.xml的活动中使用样式
<activity ....
android:theme="@style/AppTheme.NoActionBar" > ......
</activity>
。。。。。。
以上的答案将有助于操作栏。若要添加,请在使用启动屏幕时使用以下代码:
在设置内容视图之前,请使用此选项:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
只是想澄清一下,以下是您的做法:
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
这将使您的屏幕成为全屏,即删除您看到网络栏的顶部栏等这在API 27中起作用
在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>
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
然后在您确实希望有工具栏的文件(例如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"/>
如果您有问题,请切换到线性布局(因为这是测试此代码的基础)这对我很有用!将其放入清单中的活动中
<activity
// ...
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
// ...
</activity>
来自:
从Android 3.0(API 11级)开始,所有使用默认主题的活动都有一个ActionBar作为应用程序栏。然而,在各种Android版本中,应用程序栏功能已逐渐添加到本机ActionBar中。因此,根据设备可能使用的Android系统版本,本机ActionBar的行为会有所不同。相比之下,最新的功能添加到支持库版本的工具栏,可以在任何可以使用支持库的设备上使用
因此,一个选项是完全禁用ActionBar(在app manifest.xml文件中),然后在每个需要ActionBar的页面中添加工具栏
<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>
(按照上面的链接逐步解释)在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>
然后在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>
<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>
<!-- 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>
真的
假的
真的
@空的
替换AndroidManifest.xml
android:theme="@style/FullscreenTheme"
到
在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>
<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>
<!-- 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>
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
然后在清单中添加以下行
<activity android:name=".Login"
android:label="@string/app_name"
android:theme="@style/LoginTheme"
>
如果更改主题后问题仍然存在,请检查填充属性
填充问题在片段窗口/应用程序窗口顶部创建空白
删除padding-top值后,将自动删除空白。要隐藏操作栏,请将此代码添加到java文件中
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
活动标记的第一个活动,如-
公共类MainActivity扩展了AppCompatActivity
然后转到menifest xml文件并写入-
android:theme=“@style/theme.AppCompat.Light.NoActionBar”
在活动标签中有选择地隐藏活动中的Actionbar:
将以下代码添加到onCreate(最好在函数的最后一行)
科特林:
supportActionBar?.hide()
爪哇:
getSupportActionBar().hide()代码>如果活动扩展了AppCompatActivity
,则可以添加此行
super.getSupportActionBar().hide()beforesetContentView()我不能使用getActionBar
,因为该活动不是从ActionBarActivity
继承的。如果我使用false
,这会在所有屏幕上隐藏它吗?请更新我的答案,它应该更适合您的需要。+1用于向我介绍:),我将首先检查@SiKni8解决方案,因为我更喜欢非编码解决方案。没问题,我发现它对于在我的应用程序中显示全屏地图非常有用,对我来说效果非常好。我使用:getSupportActionBar().hide()代码>在ActionBarActivity中