在Android中,處理窗口Insets(邊距)以適應不同屏幕尺寸和方向是一個重要的任務。以下是一些關鍵步驟和策略,可以幫助你實現這一目標:
WindowInsetsController
從Android 11(API級別30)開始,可以使用 WindowInsetsController
來管理和自定義窗口Insets。
import android.graphics.Rect;
import android.os.Build;
import android.view.WindowInsetsController;
import android.view.WindowManager;
// 獲取WindowInsetsController
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowInsetsController windowInsetsController = windowManager.getInsetsController(getWindow().getDecorView());
}
WindowInsets
你可以通過 WindowInsets
對象獲取和處理各種邊距類型。
Rect safeAreaInsets = windowInsets.getSafeArea();
Rect contentInsets = windowInsets.getContentInsets();
Rect systemGestureInsets = windowInsets.getSystemGestureInsets();
Rect displayFrameInsets = windowInsets.getDisplayFrameInsets();
根據不同的Insets調整你的布局??梢允褂?ConstraintLayout
或 RelativeLayout
來創建靈活的布局。
<androidx.constraintlayout.widget.ConstraintLayout
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">
<View
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/safeArea" />
<View
android:id="@+id/safeArea"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/content" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果你需要處理系統手勢Insets,可以使用 systemGestureInsets
。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsController windowInsetsController = windowManager.getInsetsController(getWindow().getDecorView());
windowInsetsController.setSystemGestureInsetsRelative(new Rect(0, 0, 0, systemGestureInsets.bottom));
}
顯示幀Insets通常用于處理劉海屏和挖孔屏。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsController windowInsetsController = windowManager.getInsetsController(getWindow().getDecorView());
windowInsetsController.setDisplayFrameInsetsRelative(new Rect(0, 0, 0, displayFrameInsets.bottom));
}
你可以監聽Insets的變化,以便在Insets發生變化時更新你的布局。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsController windowInsetsController = windowManager.getInsetsController(getWindow().getDecorView());
windowInsetsController.addCallback(new WindowInsetsController.OnInsetsChangedListener() {
@Override
public void onInsetsChanged(WindowInsets windowInsets) {
Rect safeAreaInsets = windowInsets.getSafeArea();
Rect contentInsets = windowInsets.getContentInsets();
Rect systemGestureInsets = windowInsets.getSystemGestureInsets();
Rect displayFrameInsets = windowInsets.getDisplayFrameInsets();
// 更新布局
}
});
}
通過以上步驟,你可以有效地處理Android窗口Insets,以適應不同屏幕尺寸和方向。