一组可以动态适配 WindowInsets
尺寸的组件,用于快速实现沉浸式布局效果。
您可以通过阅读官方文章来了解什么是 边到边设计 。
添加依赖:
repositories {
maven { setUrl("https://jitpack.io") }
}
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("com.github.TakWolf.Android-InsetsWidget:insetswidget:0.0.1")
// 可选。为 `ConstraintLayout` 添加支持。
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.github.TakWolf.Android-InsetsWidget:constraintlayout:0.0.1")
}
在主题中添加下面的属性,使状态栏和导航栏颜色透明:
<style name="Theme.ForYourApp">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
在 API 29 以上,需要额外添加下面的配置来关闭导航栏对比度:
<style name="Theme.ForYourApp">
<item name="android:enforceNavigationBarContrast">false</item>
</style>
在 Activity
中启用延伸布局:
class SomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
setContentView(R.layout.activity_some)
}
}
最后,确保不要在你的布局中使用 android:fitsSystemWindows="true"
属性。
该属性会拦截 WindowInsets
事件,从而导致组件失效。
包括:
InsetsView
InsetsSpace
这个类型的组件尺寸会自动适配窗口附加物类型的尺寸。例如:
<com.takwolf.android.insetswidget.InsetsView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:isw_insetsType="statusBars" />
包括:
InsetsFrameLayout
InsetsLinearLayout
InsetsNestedScrollView
InsetsConstraintLayout
这个类型的组件,会自动添加 Padding
来适配窗口附加物类型的尺寸。也可以额外配置颜色。例如:
<com.takwolf.android.insetswidget.InsetsLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:isw_insetsType="statusBars|navigationBars"
app:isw_insetsPaddingBottom="false"
app:isw_insetsColorTop="@color/translucent_black"
app:isw_insetsColorBottom="@color/translucent_black">
<YourView />
</com.takwolf.android.insetswidget.InsetsLinearLayout>
Copyright 2022 TakWolf
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.