首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android 佈局讓控件緊貼 Activity 邊緣:移除預設 Padding 與 Window Insets

     編輯:程式設計綜合問答

原因分析

  • **Theme 預設 Padding** : `Theme.AppCompat` 等預設給 `contentView` 加入 `padding` (如 16dp)。
  • **Window Insets** : 狀態欄、導航欄佔用空間,`fitsSystemWindows` 未正確處理。
  • **父佈局 Padding/Margin** : `ConstraintLayout`/`LinearLayout` 等父容器設有內邊距。

解決方案

  1. 主題移除 Padding:
    <style name="AppTheme.NoPadding" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:padding">0dp</item>
    </style>
    
    在 `AndroidManifest` 對應 ``。
  2. 根佈局設定:
    <LinearLayout
        xmlns:android="https://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:fitsSystemWindows="true"> ... </LinearLayout>
    
  3. ImageView 全螢鏡背景:
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/bg"
        android:contentDescription="@string/desc" />
    
    **注意**:用 `android:background` 而非 `src` 可避免留白,`scaleType="centerCrop"` 填滿。

驗證

開啟「佈局邊界顯示」 (開發者選項) 查看紅線邊界,確認無多餘間距。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved