最近項目需要修改下拉通知欄面闆的寬度,完成後,寫個Blog做個總結,也提供給需要的開發人員參考。
本文介紹了DDMS中 Dump View Hierarchy for UI Automator 工具的使用方法,通過該工具找到一些應用的布局,快速定位我們需要修改的源碼位置。
1 先看下效果圖
修改前,橫屏狀态的下拉通知欄,距離螢幕左右兩邊還有段距離。(模拟器中的截圖,Android原生的狀态)
修改後,橫屏狀态的下拉通知欄,寬度鋪滿螢幕。(真實裝置截圖, 修改後刷機效果)
2 找到這部分的相關布局。
SystemUI下拉通知欄的布局為super_status_bar.xml
代碼如下
<!-- This is the combined status bar / notification panel window. -->
<com.android.systemui.statusbar.phone.StatusBarWindowView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.android.systemui.statusbar.BackDropView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
<ImageView android:id="@+id/backdrop_back"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:layout_height="match_parent" />
<ImageView android:id="@+id/backdrop_front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:visibility="invisible" />
</com.android.systemui.statusbar.BackDropView>
<com.android.systemui.statusbar.ScrimView android:id="@+id/scrim_behind"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no" />
<include layout="@layout/status_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_height" />
<FrameLayout android:id="@+id/brightness_mirror"
android:layout_width="@dimen/notification_panel_width"
android:layout_height="wrap_content"
android:layout_gravity="@integer/notification_panel_layout_gravity"
android:paddingLeft="@dimen/notification_side_padding"
android:paddingRight="@dimen/notification_side_padding"
android:visibility="gone">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="2dp"
android:background="@drawable/brightness_mirror_background">
<include layout="@layout/quick_settings_brightness_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
</FrameLayout>
<com.android.systemui.statusbar.phone.PanelHolder
android:id="@+id/panel_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent" >
<include layout="@layout/status_bar_expanded"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</com.android.systemui.statusbar.phone.PanelHolder>
<com.android.systemui.statusbar.ScrimView android:id="@+id/scrim_in_front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no" />
</com.android.systemui.statusbar.phone.StatusBarWindowView>
幾個關鍵的字眼:
| “@layout/status_bar” --------------> 狀态欄
| “@+id/brightness_mirror” --------> 下拉通知欄中調節亮度時,隻剩下亮度調節彈出框,位置與下拉通知欄亮度調節位置一樣的。
| “@+id/panel_holder”--------------->下拉通知欄載體
| “@layout/status_bar_expanded”->下拉通知欄布局
super_status_bar.xml包含了狀态欄,下拉通知欄等布局
3 找到下拉通知欄相關布局
通過 DDMS 的 Dump View Hierarchy for UI Automator 工具,我們可以抓取一些布局的ID。
-3.1 header
通知欄上半部分是 com.android.systemui:id/header,那我們在SystemUI的res中,搜尋這個**“header”** 。
搜尋到layout中帶有header的,有status_bar_expanded_header.xml,隻有這個布局有這個ID
<com.android.systemui.statusbar.phone.StatusBarHeaderView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:systemui="http://schemas.android.com/apk/res-auto"
android:id="@+id/header"
android:layout_width="@dimen/notification_panel_width"
android:layout_height="@dimen/status_bar_header_height"
android:layout_gravity="@integer/notification_panel_layout_gravity"
android:paddingStart="@dimen/notification_side_padding"
android:paddingEnd="@dimen/notification_side_padding"
android:baselineAligned="false"
android:elevation="4dp"
android:background="@drawable/notification_header_bg"
android:clickable="true"
android:focusable="true"
如果需要修改header的尺寸,可将
android:layout_width="@dimen/notification_panel_width"
修改為
android:layout_width="match_parent"
重新編譯,這個header的寬度就和螢幕一樣了。
-3.2 scroll_view
可上下滑動的快捷開關布局。
上圖所示的布局代碼如下
<com.android.systemui.statusbar.phone.ObservableScrollView
android:id="@+id/scroll_view"
android:layout_width="@dimen/notification_panel_width"
android:layout_height="match_parent"
android:layout_gravity="@integer/notification_panel_layout_gravity"
android:scrollbars="none"
android:overScrollMode="never"
android:fillViewport="true">
将寬度屬性改成:
android:layout_width="match_parent"
-3.3 notification_stack_scroller
通知清單布局
上圖所示的布局代碼如下
<com.android.systemui.statusbar.stack.NotificationStackScrollLayout
android:id="@+id/notification_stack_scroller"
android:layout_width="@dimen/notification_panel_width"
android:layout_height="match_parent"
android:layout_gravity="@integer/notification_panel_layout_gravity"
android:layout_marginBottom="@dimen/close_handle_underlap"
android:importantForAccessibility="no" />
寬度屬性改成:
android:layout_width="match_parent"
4 重新編譯,打包ROM
make源碼,重新刷機檢視效果,可以看到文章開頭的gif圖所示的效果
感謝閱讀,如果覺得對您有幫助的話,請點個贊,頂一個。謝謝。