Android-在某些设备上全屏显示的奇怪工件

问题描述

在Android 10模拟器和galaxy S8物理设备(运行Android 9)上运行时,下一个代码可以正常运行。不过,在进入全屏模式的Motorola Moto G8(运行Android 9)上,所有视图的内容都会下降,并且会在状态栏位置显示空白黑色空间。

galaxy G8,普通和全屏视图:

Galaxy,normal mode

@L_404_1@

Motorola Moto G8,普通和全屏视图:

Moto,normal

Moto,full screen

如您所见,在Motorola g8屏幕上,状态栏的尺寸减小,而在屏幕顶部则显示黑屏。

Moto,artifact

这是Android清单:

public class Sound {
    private static SoundPool soundPool;
    
    public short[] getSound(Activity activity,String...fileNames) {
        soundPool = new SoundPool(10,AudioManager.STREAM_MUSIC,0);
        short[] ids = new short[fileNames.length];
        activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        try {
            AssetManager assetManager = activity.getAssets();
            for(int i = 0; i < fileNames.length; i++) {
                AssetFileDescriptor descriptor = assetManager.openFd(fileNames[i]);
                ids[i] = (short) soundPool.load(descriptor,1);
            }
        } catch(IOException e) {
            //
        }
        return ids;
    }
    
    public void playSound(short id,float rV,float lV) {
        if(id != -1) {
            soundPool.play(id,rV,lV,1);
        }
    }
    
    public void pauseSound() {
        soundPool.autopause();
    }   

    
    public void releaseSound() {
        soundPool.release();
    }
}

这里是android:theme="@style/AppTheme"

styles.xml

布局文件

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>

        <!--required Api 21 above-->
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

活动类别:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:onClick="onButtonClick"
        android:src="@drawable/llama" />

</RelativeLayout>

可以在Github上找到整个项目:https://github.com/alecpetrosky/Android-Immerse-Demo

解决方法

在函数setContentView之后添加以下代码

  getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
 
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
          }