[启动画面]如何在全屏幕显示一张图片?

21

我想在我的应用程序中制作启动画面,为此,我需要知道如何在全屏显示图像。这可以通过XML或Java代码实现吗?如何实现? 目前我只做了这个:

public class SplashScreen extends Activity {

    private static final int STOPSPLASH = 0;

    private static final long SPLASHTIME = 5000;



    private Handler splashHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case STOPSPLASH:
    //remove SplashScreen from view
    Intent intent = new Intent(SplashScreen.this, jetpack.class);
    startActivity(intent);
    break;
    }
    super.handleMessage(msg);
    }
    };



    @Override

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);
    Message msg = new Message();
    msg.what = STOPSPLASH;
    splashHandler.sendMessageDelayed(msg, SPLASHTIME);
    }
    }

如何创建 splash_screen.xml 文件?谢谢你的帮助。

5个回答

36

展示全屏启动画面的最佳方法是在您的清单文件中的活动标签下添加以下行:

android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"

您也可以使用其他主题

Theme.Black.NoTitleBar.Fullscreen
Theme.NoTitleBar.Fullscreen

<activity
        android:name="com.example.SplashActivity"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

适用于最新的Lollipop及以上版本API

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

在清单文件中为闪屏活动指定这个主题:

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


1
这只是让我的标题变成了“Theme.Light.NoTitleBar.Fullscreen”,我该怎么解决? - Ruchir Baronia
1
我遇到了一个错误: "您需要在此活动中使用Theme.AppCompat主题(或其子类)"。 - Moslem Hadi
1
已编辑答案以适应新的API版本。 - Ravi

32
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:src="@drawable/image" />
</LinearLayout>

setContentView(R.layout.splash_screen); 之前添加以下代码。
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

很好的问题,但我有另一个问题,当我按返回按钮时,闪屏又会再次显示出来,我会开启另一个主题。谢谢 :)。 - androniennn
4
当您按下返回按钮时,必须调用 finish();,这样该活动将不会显示。 - user874649
同时,如果您能够将此启动屏幕运行在一个线程上,那就更好了。 - user874649
是的,我发现finish()会销毁活动,当我按返回按钮时,没有等待的活动,应用程序就会退出。那么我的代码和线程之间有什么区别呢? - androniennn
启动画面的作用不仅仅是为了设计,你还可以在启动画面加载时打开数据库连接或运行其他内容。这取决于你正在开发的应用程序。 - user874649

2
如果R.layout.splash_screen包含一个高度和宽度设置为fill_parent或match_parent(取决于版本)的图像,它将填充屏幕。

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

0

enter image description here


只需使用super.onCreatesetConntentView

代码:

/** Hiding Title bar of this activity screen */
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
/** Making this activity, full screen */
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);

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