【问题标题】:Hide Action Bar for Splash Screen隐藏启动画面的操作栏
【发布时间】:2015-01-28 21:39:07
【问题描述】:

我正在尝试创建没有操作栏的启动画面。
首先在创建启动画面之前,主要活动中的操作栏以及当我创建启动画面时,操作栏进入启动画面并且主要活动是全屏的。我搜索了getwindow()getActionBar() 之类的方法,但是当我使用这些方法时,程序对我说不幸停止了。那么我错过了什么?

如何避免在初始屏幕中出现actionBar

我的代码:

public class MainActivity extends ActionBarActivity {

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

    setContentView(R.layout.splash_item);
    
    Thread th=new Thread(){
        public void run(){
            try {
                sleep(4000);
                Intent intent=new Intent(MainActivity.this,SplashScreen.class);
                startActivity(intent);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                finish();
            }
        }
    };
    th.start();
}

清单:

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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=".SplashScreen"
        android:label="@string/app_name" 
        >
        <intent-filter>
            <action android:name="android.intent.action.SPLASHSCREEN" />

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

【问题讨论】:

标签: android android-actionbar splash-screen


【解决方案1】:

首先,在应用顶部导入支持库:

import android.support.v7.app.ActionBarActivity;

并按如下方式更改您的代码:

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

    setContentView(R.layout.splash_item);
    getSupportActionBar().hide();
}

【讨论】:

    【解决方案2】:

    使用AppCompat 支持所有版本:

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

    【讨论】:

      【解决方案3】:
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);
      

      把它放在你的 setContentView(...) 之前,应该可以完成工作。

      【讨论】:

      • 我说但不幸停止了@The Hungry Androider
      • 这不适用于 Android 5.0 它只隐藏状态栏
      【解决方案4】:

      对于 Android 3.0 或更高版本,请使用 ActionBarAPI#hide
      对于较低版本,您将需要使用 Android 支持库。 使用 ActionBar 作为

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

      参考docs

      此外,如果您的要求是静态的,那么您可以为您的活动选择一个没有操作栏的主题,例如

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

      你可以这样做:

      <activity android:theme="@android:style/Theme.Holo.Light.NoActionBar" 
        android:name=".name_here" 
        android:label="@string/app_name" >
      

      【讨论】:

        【解决方案5】:

        使用不带操作栏的样式,也在您的初始屏幕活动中使用 java 扩展 Activity 并使初始屏幕成为您的主要活动,并在此活动中调用 Intent 以在几秒钟后打开您的 MainActivity

         <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
            </activity>
                <activity android:theme="@android:style/Theme.Holo.Light.NoActionBar"
                android:name=".SplashScreen"
                android:label="@string/app_name" >
               <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
        
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
        

        示例: 在您的 SplashScreen 活动中编写此代码。这将在 2 秒后打开您的 MainActivity。

          new Handler().postDelayed(new Runnable()
          {
            public void run()
            {
              Intent localIntent = new Intent(SplashScreen.this, MainActivity.class);
              SplashScreen.this.startActivity(localIntent);
              SplashScreen.this.finish();
            }
          }, 2000L);
        

        【讨论】:

          【解决方案6】:

          这是最简单的方法。

          @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              requestWindowFeature(Window.FEATURE_NO_TITLE);
              getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                          WindowManager.LayoutParams.FLAG_FULLSCREEN);
              setContentView(R.layout.activity_main);
          }
          

          其中 R.layout.activity_main 被您的布局活动替换,例如活动飞溅

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-05-18
            • 1970-01-01
            • 2021-05-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多