Android NavigationBar 代码分析记录(一)
2017-06-01 本文已影响0人
铁皮阿童木
NavigationBar 源码地址:
http://androidxref.com/5.1.0_r1/xref/frameworks/base/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/bars/NavigationBar.java
这是androidxref的源码,最新的源码可以在github/android上看。
阅读源码可知:
//noinspection SpellCheckingInspection
loadIcon(back, "ic_sysbar_back.png", density, isRtl);
//noinspection SpellCheckingInspection
loadIcon(2, "ic_sysbar_home.png", density, isRtl);
//noinspection SpellCheckingInspection
loadIcon(recent, "ic_sysbar_recent.png", density, isRtl);
导航栏底部三个按钮由这三个方法加载完成
方法有四个参数 默认第一个按钮为back=1,第三个为recent=3,此处基于5.1.0的源码,github中android最新的源码back=1,recent=5
int back = 1;
int recent = 3;
if (orientation == LinearLayout.VERTICAL || (isRtl && !rtlEnabled)) {
// If RTL is enabled, then layoutlib mirrors the layout for us.
back = 3;
recent = 1;
}
if中的Rtl是 right to left,android开发者模式里有一个强制右到左布局的功能,这个boolean应该是对应这个,而loadicon里的第一个参数应该是在navigationbar的位置。
下面是布局的部分源码,为了方便阅读,我去掉了一部分与分析无关的内容,
源码地址
http://androidxref.com/5.1.0_r1/xref/frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml
<com.android.systemui.statusbar.phone.NavigationBarView
android:background="@drawable/system_bar_background">
<FrameLayout
android:id="@+id/rot0"
>
<LinearLayout
android:orientation="horizontal"
android:clipChildren="false"
android:clipToPadding="false"
android:id="@+id/nav_buttons"
android:animateLayoutChanges="true"><!-- navigation controls -->
按钮间的间距,后面也有,为了方便阅读,后面的部分去掉
<View
android:layout_width="@dimen/navigation_side_padding"
android:layout_height="match_parent"
android:layout_weight="0"
android:visibility="invisible" />
返回按钮
<com.android.systemui.statusbar.policy.KeyButtonView
android:id="@+id/back"
android:src="@drawable/ic_sysbar_back"
systemui:keyCode="4"
android:scaleType="center"
android:contentDescription="@string/accessibility_back" />
home按钮
<com.android.systemui.statusbar.policy.KeyButtonView
android:id="@+id/home"
android:src="@drawable/ic_sysbar_home"
systemui:keyCode="3"
systemui:keyRepeat="false"
android:scaleType="center"
android:contentDescription="@string/accessibility_home" />
最近运行的app按钮
<com.android.systemui.statusbar.policy.KeyButtonView
android:id="@+id/recent_apps"
android:src="@drawable/ic_sysbar_recent"
android:scaleType="center"
android:contentDescription="@string/accessibility_recent" />
菜单按钮 和 选择键盘 按钮 默认是隐藏的
<FrameLayout
android:layout_weight="0">
<com.android.systemui.statusbar.policy.KeyButtonView
android:id="@+id/menu"
android:contentDescription="@string/accessibility_menu"
android:src="@drawable/ic_sysbar_menu"
android:visibility="invisible"
android:scaleType="centerInside"
android:layout_gravity="end"
systemui:keyCode="82" />
<com.android.systemui.statusbar.policy.KeyButtonView
android:id="@+id/ime_switcher"
android:contentDescription="@string/accessibility_ime_switch_button"
android:scaleType="centerInside"
android:src="@drawable/ic_ime_switcher_default"
android:visibility="invisible"
android:layout_gravity="end" />
</FrameLayout>
某些操作时,隐藏藏本来的三个按钮,替换成三个小点,这个情况我只在nexus自带的相机软件里面看到过
</LinearLayout><!-- lights out layout to match exactly -->
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="@+id/lights_out"
android:visibility="gone">
<ImageView
android:layout_width="@dimen/navigation_key_width"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/navigation_side_padding"
android:src="@drawable/ic_sysbar_lights_out_dot_small"
android:scaleType="center"
android:layout_weight="0" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:visibility="invisible" />
<ImageView
android:layout_width="@dimen/navigation_key_width"
android:layout_height="match_parent"
android:src="@drawable/ic_sysbar_lights_out_dot_large"
android:scaleType="center"
android:layout_weight="0" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:visibility="invisible" />
<ImageView
android:layout_width="@dimen/navigation_key_width"
android:layout_marginEnd="@dimen/navigation_side_padding"
android:layout_height="match_parent"
android:src="@drawable/ic_sysbar_lights_out_dot_small"
android:scaleType="center"
android:layout_weight="0" />
</LinearLayout>
全屏等情况出现避免误触?
<com.android.systemui.statusbar.policy.DeadZone
android:id="@+id/deadzone"
android:layout_height="match_parent"
android:layout_width="match_parent"
systemui:minSize="@dimen/navigation_bar_deadzone_size"
systemui:maxSize="@dimen/navigation_bar_deadzone_size_max"
systemui:holdTime="@integer/navigation_bar_deadzone_hold"
systemui:decayTime="@integer/navigation_bar_deadzone_decay"
systemui:orientation="horizontal"
android:layout_gravity="top" />
</FrameLayout>