Android开机画面切换

时间:2019-02-18 16:31:32

标签: android react-native

我有一个Android Splash Screen,可绘制一个可绘制图形。通过冷启动将其打开时,我发现我的资产只是在向上移动。

您可以在下面找到合适的代码,所有不必要的代码都已被省略。

这里是轻微的变化:

enter image description here

SplashActivity.java

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this, R.style.SplashTheme);
    super.onCreate(savedInstanceState);
}

res / drawable / background_splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <item android:drawable="@color/splash_background_color"/>
    <item
        android:gravity="center"
        >
        <bitmap
            android:src="@drawable/x150"/>
    </item>
</layer-list>

res / layout / launch_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/splash_background_color">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/background_splash"
        />
</FrameLayout>

res / values / styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>
</resources>

3 个答案:

答案 0 :(得分:2)

我找到了解决方法:

您应该删除ImageView,因为您已经通过android:windowBackground设置了初始设置。 还要从android:background="@color/splash_background_color"中删除FrameLayout以使其透明

顺便说一句,如果您不打算在启动画面上绘制一些布局,则可以删除res/layout/launch_screen.xml

对于Activity,请不要拨打setContentView()

对于Fragment,请不要覆盖onCreateView()

没关系,Android不需要为其设置布局。

答案 1 :(得分:0)

styles.xml中,替换:

<item name="android:windowBackground">@drawable/background_splash</item>

使用

<item name="android:background">@drawable/background_splash</item>

请注意windowBackground -> background

那为我解决了这个问题。

答案 2 :(得分:0)

奇怪,但我的启动画面也面临相同的问题。在完成一些任务之后并在打开主要活动时,它正在向上移动。

但是在尝试了几种组合之后-基本上,我在显示初始屏幕时添加了几行代码来获取一些数据,并为此添加了一个onDestroy替代项来取消我的截击请求队列。

@Override
    protected void onDestroy() {
        super.onDestroy();
        if (queue!= null) {
            queue.cancelAll(Constant.REQUEST_Q);
        }
    }

一旦我删除了上面的onDestroy,就会覆盖启动画面,停止移动或向上移动。我无法解释为什么,在此搜索答案时,这是我的第一篇帖子。

相关问题