安卓启动画面偏移问题

13

我有一个Android启动屏幕,它渲染了一个drawable。当通过冷启动打开时,我发现我的资源只是向上移动。

下面是相关的代码,所有不必要的代码都已省略。

这里是轻微的位移:

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>

可能是因为您的状态栏是透明的。尝试在根FrameLayout中添加android:fitsSystemWindows="true" - Taras Parshenko
@TarasParshenko 谢谢您的建议,不幸的是它仍然存在于 android:fitsSystemWindows="true"> 中。 - Dan
嘿,我遇到了类似的错误,请问你能告诉我在SplashActivity.java中导入AppCompatActivity的代码行吗? - hashinclude72
我也是。Android Pixel 3a不错,但Galaxy S10更胜一筹! - shinriyo
2个回答

7
我找到了解决办法:
你应该删除ImageView,因为您已经通过android:windowBackground设置了启动画面。 同时,从FrameLayout中删除android:background="@color/splash_background_color"以使其透明。
另外,如果您不打算在启动画面上绘制一些布局,则可以删除res/layout/launch_screen.xml
对于Activity不要调用setContentView() 对于Fragment不要重写onCreateView() 没问题,Android不需要为它们设置布局。

3
去掉这个值意味着第一次不会渲染,它会显示一个白屏,然后再显示标志。与持续显示标志相反。 - Dan
在这种情况下,可以删除ImageView。问题出在android:windowBackground<ImageView android:layout_gravity="center">的渲染方式不同。看起来windowBackground有更多的空间,因此位图向上移动。 - Taras Parshenko
如果我理解正确的话,现在移除 ImageView 将不会呈现任何内容。 - Dan
是的,你还应该从FrameLayout中删除android:background="@color/splash_background_color"以使其透明。 - Taras Parshenko
非常感谢!如果您能更新您的答案并包含评论中提到的内容,我会给您点赞和采纳。还有一个问题,如果“launch_screen.xml”只呈现透明的FrameLayout,是否需要该文件? - Dan
无边框设备问题?? 我在“android/app/src/main/res/values/styles.xml”中将样式/主题。黑色。NoTitleBar更改为样式/主题。NoTitleBar.Fullscreen。 https://github.com/apache/cordova-android/issues/844 - shinriyo

4
styles.xml文件中,将以下内容替换:
<item name="android:windowBackground">@drawable/background_splash</item>

使用

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

请注意windowBackground属性的作用,它对应着background属性。
这样我就解决了我的问题。

2
你是最棒的。 - Shikhar

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