Android P 启动页适配适配刘海屏
2021-04-06 本文已影响0人
李云龙_
需求: app 启动页全屏,延伸到刘海屏的状态栏和底部的导航栏,操作步骤如下:
app 的 build.gradle 配置
注意 compileSdkVersion、buildToolsVersion、targetSdkVersion 都是 30
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.toys.testimmersionbar"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
api 'com.gyf.barlibrary:barlibrary:2.3.0'
implementation 'com.google.android.material:material:1.1.0-alpha05'
}
启动页 SplashActivity 代码
注意: SplashActivity 不需要 setContentView( )
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_splash3)
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
StatusBarCompat.setTranslucentDiff(this)
StatusBarCompat.setNavigationBarTransparent(this)
Handler().postDelayed({
startActivity(Intent(this, MainActivity::class.java))
}, 3000)
}
}
AndroidManifest.xml 配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.toys.testimmersionbar">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<activity android:name=".activity.MainActivity" />
<activity
android:name=".activity.SplashActivity3"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 华为刘海屏区域使用 -->
<meta-data
android:name="android.notch_support"
android:value="true" />
</activity>
</application>
</manifest>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Splash" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/white</item>
<item name="colorPrimaryDark">@color/white</item>
<item name="colorAccent">@color/white</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowBackground">@drawable/splash_logo_bg</item>
<!-- <item name="android:windowAnimationStyle">@style/ActivityAnimationFadeIn</item>-->
<item name="android:windowFullscreen">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
splash_logo_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="@color/bg_splash" />
<!--顶部 girl-->
<item
android:top="227dp">
<bitmap
android:gravity="top"
android:src="@drawable/mc_forum_face234" />
</item>
</layer-list>
splash_logo_bg.xml
的预览效果如下:
最终效果预览
image可见启动页完全全屏了