启动画面奇怪的黑色矩形在屏幕上

Splash Screen strange Black Rectangle on screen

UPDATE: Finally find a little bit hacky solution. But it's not acceptable for me. All ideas can be useful.

我有启动画面Activity,但我没有使用 setContentView()。 我使用 android:windowBackground

styles.xml 设置背景

当初始屏幕启动时,一个黑色矩形不知从哪里出现,位于屏幕的左下方。高度与 NavigationBar 的高度完全相同,从屏幕左侧开始到屏幕中心结束。

这个黑色矩形只出现有导航栏的设备上。不是物理按钮。并且也仅在 FullScreen activity.

下尝试 隐藏或显示导航栏 时可见

QUESTION: What can be the reason of this bug? How can I solve it?

您可以在下面看到整个事件。 (初始屏幕到登录屏幕的过渡)

这里是我的splashactivity风格:

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@drawable/background_splash</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    //Tried different colored layer at bottom level but did not affect
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/grey_Dark"/>
        </shape>
    </item>

    //Background image
    <item
        android:drawable="@drawable/bg"
        android:gravity="center"/>

    //9-patch Logo at the center
    <item
        android:drawable="@drawable/splash_logo"
        android:gravity="center"/>

</layer-list>

Activity飞溅

public class ActivitySplash extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(ActivitySplash.this, ActivityLogin.class);
                startActivity(intent);
                finish();
            }
        }, 2000);

    }
}

终于找到了一个有点老套的解决方案。但这对我来说是不能接受的。任何想法都可能有用。

首先,使用 Layout Inspector 得到这个结果:

DecorView下,有一个不需要的奇怪View是在启动splash后创建的activity.

闪烁半秒后消失

闪烁后,如果用户触发屏幕显示导航栏,这个奇怪的视图会再次出现(永久)。

添加后闪烁视图消失

<item name="android:windowIsTranslucent">true</item>

我的活动风格。

其他部分比较复杂。 向 activity 的 DecorView 添加了 OnHierarchyChangeListener。将我的 DecorView 转换为 FrameLayout,并覆盖 onChildViewAdded() 回调以在添加到 DecorView.

后删除所有子项
final FrameLayout myDecorView = (FrameLayout) decorView;
myDecorView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
    @Override
    public void onChildViewAdded(View parent, View child) {
        myDecorView.removeView(child);
    }

    @Override
    public void onChildViewRemoved(View parent, View child) {

    }
});