fitsSystemWindows之大坑

fitsSystemWindows通常我們用來實現(xiàn)各版本來狀態(tài)欄的適配(API19以上我們才能修改狀態(tài)欄),但在使用過程中,發(fā)現(xiàn)使用不當(dāng)會給自己挖下很大的坑。先看一下官方描述:

Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.
May be a boolean value, such as "true" or "false".

布爾內(nèi)部屬性,用于根據(jù)系統(tǒng)窗口(如狀態(tài)欄)調(diào)整視圖布局。如果為true,則調(diào)整此視圖的填充以為系統(tǒng)窗口留出空間。僅在此視圖位于非嵌入活動中時才會生效。

fitsSystemWindows屬性的使用,要想生效必須與透明狀態(tài)欄或透明底部導(dǎo)航欄一起使用方可生效。

fitsSystemWindows = true時,會使我們的布局或View預(yù)留出系統(tǒng)狀態(tài)欄底部導(dǎo)航欄空間,即給View自動添加上padding值,這時候我們給View設(shè)置的padding屬性則無效

坑一:

1.不設(shè)置透明狀態(tài)欄或透明底部導(dǎo)航欄,雖然不生效,但在部分機(jī)型上EditText長按粘貼時會出現(xiàn)適配問題,如華為手機(jī)上彈出緊包裹粘貼文案的彈窗,模擬器上出現(xiàn)布局不居中彈窗等問題
2.只要設(shè)置了透明狀態(tài)欄或透明底部導(dǎo)航欄,即可生效,但我們設(shè)置的padding屬性將不再生效

坑一.png
 <item name="android:fitsSystemWindows">true</item>
<item name="android:windowTranslucentStatus">true</item>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/holo_blue_bright">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

fitsSystemWindows = false時,我們的布局會占據(jù)整個系統(tǒng)窗口,不會給狀態(tài)欄、底部導(dǎo)航欄預(yù)留空間,當(dāng)然前提是:必須設(shè)置透明狀態(tài)欄或透明底部導(dǎo)航欄,這樣方可生效。(通常我們使用其實現(xiàn)沉浸式)

坑二:

如果我們設(shè)置了底部透明狀態(tài)欄時,這時候我們布局占據(jù)整個系統(tǒng)窗口,如果我們布局底部存在tab切換按鈕時,如果我們的手機(jī)是虛擬底部導(dǎo)航欄。導(dǎo)航欄會覆蓋在我們的tab按鈕上,導(dǎo)致我們的tab按鈕無效

不過fitsSystemWindows = false時,不管是否生效,我們給布局設(shè)置的padding屬性是生效的,另外對于EditText的長按粘貼也正常了。
坑二.png
 <item name="android:fitsSystemWindows">false</item>
 <item name="android:windowTranslucentStatus">true</item>
 <item name="android:windowTranslucentNavigation">true</item>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/holo_blue_bright">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>
對于fitsSystemWindows可真的小心使用,個人不建議使用fitsSystemWindows=true,至于fitsSystemWindows=false我們可視情況使用,如果想達(dá)到不通過設(shè)置fitsSystemWindows而實現(xiàn)沉浸式效果,可參考StatusBarUtils沉浸式狀態(tài)欄適配(第二種實現(xiàn)方式)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容