Android啟動頁,歡迎頁Splash頁面,劉海全面屏沉浸式白屏解決

Android啟動頁,歡迎頁Splash頁面,劉海全面屏沉浸式白屏

隨著iPhone X的發布,一眾Android廠商也開始跟風推出自己的劉海屏,全面屏手機,整個手機行業已經從傳統的16:9逐漸往18:9,18.5:9等屏幕比例過渡,其中也給我們開發著帶來了一個比較棘手的問題,那就是Splash Screen對全面屏手機的適配問題。市面上通用的方案就是創建適配多種分辨率的drawable文件夾,這種方案有缺點明顯,不能完美的適配各種寬高比的屏幕。于是乎就有了下面這種方案就是使用layer-list來進行適配,先看效果:

16:9屏幕全面屏不帶劉海全面屏帶劉海

在drawable目錄創建一個layer-list,如splash.xml

<?xml version="1.0"encoding="utf-8"?>

<!-- 背景顏色 -->

<!--注意此處的bottom要和activity_splash.xml中的logo圖marginBottom的值保持一致-->

<!-- 圖片 -->

android:gravity="center|bottom"

android:src="@drawable/splash_logo"/>

在style.xml中為SplashActivity創建一個theme

@drawable/splash

true

在values-v21中為SplashActivity創建一個和SplashTheme同名的一個theme

創建這個theme的主要目的是為了適配有navigation bar的手機,不讓windowBackground延申到navigationBar的區域內

@drawable/splash

true

<!--不讓windowBackground延申到navigation bar區域-->

false

在AndroidManifest.xml中定義SplashActivity的theme為SplashTheme

android:name=".SplashActivity"

android:theme="@style/SplashTheme">

SplashActivity布局(參考)

需注意的是logo占位圖的marginBottom要與上面layer-list中的bottom屬性的值保持一致,這就可視覺上和windowBackground中的logo的位置保持完全一致。使得下面的logo在任意一種屏幕下都能保持居中的位置。

<?xml version="1.0"encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/iv_ad"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:contentDescription="廣告圖"

android:scaleType="centerCrop"

android:src="@drawable/ad_img"/>

android:id="@+id/logo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="40dp"

android:layout_marginBottom="40dp"

android:src="@drawable/splash_logo"

android:contentDescription="logo圖,占位"/>

劉海屏適配

需解決的問題是讓布局延申到劉海(狀態欄)區域,方法如下:

針對Android p,在values-v28中為SplashActivity創建一個和SplashTheme同名的一個theme,

@drawable/splash

true

<!--不讓windowBackground延申到navigation bar區域-->

false

<!--適配Android P劉海屏-->

shortEdges

SplashActivity的onCreate中

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

? ? Window window = getWindow();

? ? View decorView = window.getDecorView();

? ? decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {

@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)

@Override

publicWindowInsets onApplyWindowInsets(View v, WindowInsets insets) {

? ? ? ? ? ? WindowInsets defaultInsets = v.onApplyWindowInsets(insets);

returndefaultInsets.replaceSystemWindowInsets(

? ? ? ? ? ? ? ? ? ? defaultInsets.getSystemWindowInsetLeft(),

0,

? ? ? ? ? ? ? ? ? ? defaultInsets.getSystemWindowInsetRight(),

? ? ? ? ? ? ? ? ? ? defaultInsets.getSystemWindowInsetBottom());

? ? ? ? }

? ? });

? ? ViewCompat.requestApplyInsets(decorView);

//將狀態欄設成透明,如不想透明可設置其他顏色

window.setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));

}

華為手機

在AndroidManifest.xml中的SplashActivity的節點添加如下meta-data


轉載:http://www.lxweimin.com/p/105885c44e49

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。