我正在使我的项目能够处理缺口/切口,因为用户在横向模式下可以看到大的黑色矩形区域。由于我的应用程序的布局在拉伸后将是安全的,因此我决定使布局适合宽度,包括缺口的宽度。
当我使用短边模式时,应用程序的屏幕本身似乎会拉伸,但布局视图(如RelativeLayout
orConstraintLayout
等)不会跟随窗口的宽度。
我可以确定窗口被拉伸了,因为黑色矩形现在变成了白色矩形,它与我的应用程序的窗口默认背景颜色相同。
我测试了两种拉伸方式
1. 添加android:windowLayoutInDisplayCutoutMode
到style.xml
<style name="AppTheme.night" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="o_mr1">shortEdges</item>
<item name="colorPrimary">@color/colorPrimaryN</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkN</item>
<item name="colorAccent">@color/colorAccentN</item>
...
</style>
我不知道targetApi
标签是否会使事情不起作用,但这使应用程序显示白色矩形,但没有使布局视图拉伸
2. 以编程方式设置短边模式
val att = window.attributes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
att.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
似乎我可以在我的活动类中以编程方式设置短边模式,所以我尝试使用上面的代码,但结果与第一种方式相同。我什至尝试在调用之前setContentView
和调用之后移动这些行setContentView
,但结果仍然相同。
我尝试使用下面的代码将全屏模式设置为,但结果就像这张图片
@Suppress("DEPRECATION")
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val controller = window.insetsController
if(controller != null) {
controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
<?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"
android:background="?attr/backgroundPrimary"
tools:context=".CheckUpdateScreen">
<ProgressBar
android:id="@+id/prog"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="false"
android:layout_centerVertical="true"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp" />
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/prog"
android:layout_alignStart="@+id/prog"
android:layout_alignBottom="@+id/retry"
android:layout_marginStart="0dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:layout_toStartOf="@+id/retry"
android:gravity="center_vertical"
android:text="@string/down_state_rea"
android:textColor="?attr/TextPrimary"
android:textSize="18sp" />
<Button
android:id="@+id/retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/prog"
android:layout_alignEnd="@+id/prog"
android:layout_marginTop="16dp"
android:layout_marginEnd="0dp"
android:backgroundTint="?attr/ButtonPrimary"
android:text="@string/down_redown"
android:textAllCaps="false"
android:textColor="?attr/TextPrimary" />
</RelativeLayout>
这是我的屏幕布局 xml 文件(上图)
我可能会误解一些东西,因为这是第一次处理缺口/切口的东西。即使设置了短边模式,我也无法拉伸布局视图是否正常?或者是否有任何特殊的布局视图可以处理缺口/切口拉伸?