闪屏应用和隐藏操作栏

27

我正在尝试找出如何在启动屏幕活动中隐藏操作栏。我做了一些事情来隐藏我的启动屏幕上的操作栏,但是有一个问题:在我的启动屏幕出现之前,会出现一个带有操作栏的活动......我不知道如何隐藏它!

我的启动屏幕只会在第一次触摸应用程序时出现,就像应用程序的介绍。

代码:

package com.example.basicmaponline;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class Intro extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();

    setContentView(R.layout.intro);

    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(3000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }finally{
                Intent openMenu = new Intent("com.example.basicmaponline.MENU");
                startActivity(openMenu);
            }
        }
    };
    timer.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.basicmaponline"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/lojacidadao"
    android:label="@string/app_name"
     android:theme="@style/AppTheme">
    <activity
        android:name="com.example.basicmaponline.Intro"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name="com.example.basicmaponline.Menu"
        android:screenOrientation="portrait"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.MENU" />

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

    <activity
        android:name="com.example.basicmaponline.Mapa"
        android:screenOrientation="portrait"
        android:label="@string/map_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.MAPA" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.basicmaponline.Lojas"
        android:screenOrientation="portrait"
        android:label="@string/lojas_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.LOJAS" />

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

    <activity
        android:name="com.example.basicmaponline.InfoLoja"
        android:screenOrientation="portrait"
        android:label="@string/loja_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.INFOLOJA" />

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

    <activity
        android:name="com.example.basicmaponline.Balcoes"
        android:screenOrientation="portrait"
        android:label="@string/balcoes_name" >
        <intent-filter>
            <action android:name="com.example.basicmaponline.BALCOES" />

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

    <activity
        android:name="com.example.basicmaponline.Entidade"
        android:screenOrientation="portrait"
        android:label="@string/balcao_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.ENTIDADE" />

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

     <activity
        android:name="com.example.basicmaponline.Servicos"
        android:screenOrientation="portrait"
        android:label="@string/servicos_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.SERVICOS" />

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

     <activity
        android:name="com.example.basicmaponline.InfoServico"
        android:screenOrientation="portrait"
        android:label="@string/servico_name">
        <intent-filter>
            <action android:name="com.example.basicmaponline.INFOSERVICO" />

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

     <activity
        android:name="com.example.basicmaponline.Pesquisar"
        android:screenOrientation="portrait"
        android:label="@string/pesquisa_name" >
        <intent-filter>
            <action android:name="com.example.basicmaponline.PESQUISAR" />

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

</application>
</manifest>

@rciovati 我也同意这个观点。如果你首先将其设置在布局中不出现,那么你就不会遇到你所描述的闪烁问题了。 - Jay Snayder
我添加了我的清单 :) - Damiii
5个回答

62

为什么不在清单文件中添加第一个活动的主题,以排除操作栏,甚至通知栏呢?

类似这样:

<application
    android:allowBackup="true"
    android:icon="@drawable/lojacidadao"
    android:label="@string/app_name"
     android:theme="@style/AppTheme">
    <activity
        android:name="com.example.basicmaponline.Intro"
        android:screenOrientation="portrait"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

如果您扩展了普通的Activity类,这应该可以工作。

如果您的活动扩展了AppCompatActivity,则可以使用此主题作为示例:

  <style name="AppTheme.Splash" parent="@style/Theme.AppCompat.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
  </style>

含义:

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

顺便说一下,你之所以有操作栏是因为你将默认主题设置为拥有它,在应用标签中设置,因此它不是在你之前的活动中,而是真正属于你自己的启动活动。


1
成功了!谢谢! 所以来自Android的“android:Theme.Holo”将为每个活动放置操作栏。好的! :) - Damiii
是的,如果您希望支持在旧版Android(姜饼及以下版本)中添加操作栏,您可以使用ActionBarSherlock库,但当然也要使用它们的样式和类。 - android developer
我尝试了这种方法,但它改变了我的主题为黑色背景,而我的主题是**android:theme="@style/Theme.AppCompat.Light.DarkActionBar"**,为什么会发生这种情况? - Jiazzy user
@Jiazzyuser 你还应该将主题设置到应用程序标签本身。 - android developer
出现java.lang.IllegalStateException异常:您需要在此活动中使用Theme.AppCompat主题(或其后代)。 - Ananta Prasad
显示剩余2条评论

7

如果您的构建目标sdk为5.0或更高版本(AppTheme样式为Theme.AppCompat.Light.DarkActionBar.)

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

2
你的问题出在这些行。
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();

它们不需要,因为根本就没有需要调用的ActionBar,启动屏幕不使用它,并且应该是与其他屏幕完全分开的一个单独的Activity

你需要两个Activities。一个用于启动屏幕,具有自己的布局。第二个是主要的Activity,可以是登录屏幕或欢迎屏幕等。

启动屏幕类应该像这样

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.yourlayoutfile);

    Thread loading = new Thread() {
        public void run() {
            try {
                sleep(5000);
                Intent main = new Intent(SplashScreen.this, Main.class);
                startActivity(main);
            }

            catch (Exception e) {
                e.printStackTrace();
            }

            finally {
                finish();
            }
        }
    };

    loading.start();
}

}

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"    
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"    
tools:context=".SplashScreen" >

// if not using an image replace with whatever is to be shown on the splash screen
<ImageView  
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo" />

</LinearLayout>

现在第二个Activity已经拥有了包含ActionBar等元素的布局。

我做了你做的事情,它也做了同样的事情... 我点了一下我的应用程序,一个带有操作栏的活动界面短暂地出现了(半秒钟),然后我的启动屏幕出现了。 - Damiii
这是在实际设备上发生还是在AVD上? - i_me_mine
我正在使用我的三星Galaxy S4进行操作。 - Damiii
啊,我刚注意到另一个答案。看起来他找到了你问题的根源。很高兴你得到了答案,干杯! - i_me_mine

1
小而精,简单易懂。
在OnCreate中执行。
View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

1
由于这是您的闪屏界面,即使有人触摸屏幕,您可能仍希望保持操作栏隐藏。但是,采用此方法后,触摸屏幕上的任何位置都会导致操作栏重新出现并保持可见。 - Tariq

0
//add to AndroidManifest for SplashScreen
<activity
         android:name="<YOUR_PACKAGENAME.ACTIVITY>"
         android:theme="@style/AppTheme.NoActionBar"
         .........../>
//add this styles to  styles.xml

 <style name="AppTheme.NoActionBar">
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowDisablePreview">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

//add this code to Activity
public class SplashScreen extends AppCompatActivity {
.
.
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    .
    .
    .
    }

}

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