关于导航栏这一块按钮的增加其实网上也有很多的例子了,最多的可能就是一个关于音量加减的例子。
最近客户有要求做这么一个功能,即在底部导航栏再添加一个按钮,通过这个按钮来实现导航栏虚拟按键的隐藏与显示。但无论导航栏隐藏与否,这个按钮都不会消失,也就是会常驻导航栏。
我们知道关于通知栏、导航栏、下拉菜单实质上是一个SystemUI.apk。这部分的源码位于:/frameworks/base/packages/SystemUI
在网上很多关于SystemUI讲解的例子,在此我也不献丑了。
控制底部导航栏的xml文件是:navigation_bar.xml。位于:frameworks\base\packages\SystemUI\res\layout-sw600dp\navigation_bar.xml
注意这个xml在res目录下的很多文件夹内都存在,之前我改的地方是layout目录,无论怎么修改都无法让图片显示,在这里滞留了很久,最后与同事沟通,才改对地方。
话不多说,直接上代码了。
diff --git a/packages/SystemUI/res/layout-sw600dp/navigation_bar.xml b/packages/SystemUI/res/layout-sw600dp/navigation_bar.xml
index b576dfa..e275fb1 100755
--- a/packages/SystemUI/res/layout-sw600dp/navigation_bar.xml
+++ b/packages/SystemUI/res/layout-sw600dp/navigation_bar.xml
@@ -108,6 +108,18 @@
android:visibility="invisible"
systemui:keyCode="24"
/>
+
+
+ android:layout_width="120dp"
+ android:layout_height="match_parent"
+ android:layout_weight="0"
+ android:paddingLeft="0dp"
+ android:paddingRight="0dp"
+ android:scaleType="centerInside"
+ android:src="@drawable/ic_sysbar_hide_down"
+ android:clickable="true"
+ />
+
@@ -288,6 +300,18 @@
android:visibility="invisible"
systemui:keyCode="24"
/>
+
+
+ android:layout_width="84dp"
+ android:layout_height="match_parent"
+ android:layout_weight="0"
+ android:paddingLeft="0dp"
+ android:paddingRight="0dp"
+ android:scaleType="centerInside"
+ android:src="@drawable/ic_sysbar_hide_down"
+ android:clickable="true"
+ />
+
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/Nav
index 68973e6..84cc3b4 100755
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -77,7 +77,7 @@ public class NavigationBarView extends LinearLayout {
int mDisabledFlags = 0;
int mNavigationIconHints = 0;
- private Drawable mBackIcon, mBackLandIcon, mBackAltIcon, mBackAltLandIcon;
+ private Drawable mBackIcon, mBackLandIcon, mBackAltIcon, mBackAltLandIcon,down,up;
private Drawable mRecentIcon;
private Drawable mRecentLandIcon;
@@ -269,6 +269,10 @@ public class NavigationBarView extends LinearLayout {
public View getScreenshotButton(){
return mCurrentView.findViewById(R.id.screenshot);
}
+ public View gethideButton() {
+ return mCurrentView.findViewById(R.id.hide);
+ }
private void getIcons(Resources res) {
mBackIcon = res.getDrawable(R.drawable.ic_sysbar_back);
@@ -278,6 +282,13 @@ public class NavigationBarView extends LinearLayout {
mRecentIcon = res.getDrawable(R.drawable.ic_sysbar_recent);
mRecentLandIcon = mRecentIcon;
}
+ public void downIcons(Resources res) {
+ ((ImageView)gethideButton()).setImageDrawable(res.getDrawable(R.drawable.ic_sysbar_hide_down));
+ }
+ public void upIcons(Resources res) {
+ ((ImageView)gethideButton()).setImageDrawable(res.getDrawable(R.drawable.ic_sysbar_hide_up));
+ }
@Override
public void setLayoutDirection(int layoutDirection) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneS
index 09b9e5d..8e1bf27 100755
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -16,7 +16,6 @@
package com.android.systemui.statusbar.phone;
-
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.NonNull;
@@ -200,6 +199,9 @@ import android.os.Handler;
import android.util.Log;
import com.android.systemui.screenshot.ScreenshotUtils;
+import android.graphics.drawable.Drawable;
+
public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
DragDownHelper.DragDownCallback, ActivityStarter, OnUnlockMethodChangedListener,
HeadsUpManager.OnHeadsUpChangedListener {
@@ -1202,6 +1204,32 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
toggleRecentApps();
}
};
+
+ //Drawable down =mContext.getResources().getDrawable(R.drawable.ic_sysbar_hide_down);
+ //Drawable up =mContext.getResources().getDrawable(R.drawable.ic_sysbar_hide_up);
+ private boolean upordown =true;
+ private View.OnClickListener mhideButton = new View.OnClickListener() {
+ public void onClick(View v) {
+ if(upordown==true){
+ mNavigationBarView.getRecentsButton().setVisibility(View.INVISIBLE);
+ mNavigationBarView.getHomeButton().setVisibility(View.INVISIBLE);
+ mNavigationBarView.getBackButton().setVisibility(View.INVISIBLE);
+ mNavigationBarView.getVolumeAddButton().setVisibility(View.INVISIBLE);
+ mNavigationBarView.getVolumeSubButton().setVisibility(View.INVISIBLE);
+ mNavigationBarView.upIcons(mContext.getResources());
+ upordown=false;
+ }else{
+ mNavigationBarView.getRecentsButton().setVisibility(View.VISIBLE);
+ mNavigationBarView.getHomeButton().setVisibility(View.VISIBLE);
+ mNavigationBarView.getBackButton().setVisibility(View.VISIBLE);
+ mNavigationBarView.getVolumeAddButton().setVisibility(View.VISIBLE);
+ mNavigationBarView.getVolumeSubButton().setVisibility(View.VISIBLE);
+ mNavigationBarView.downIcons(mContext.getResources());
+ upordown=true;
+ }
+ }
+ };
private long mLastLockToAppLongPress;
private View.OnLongClickListener mLongPressBackRecentsListener =
@@ -1281,6 +1309,8 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
mNavigationBarView.getScreenshotButton().setVisibility(View.INVISIBLE);
}
mNavigationBarView.getScreenshotButton().setOnTouchListener(mScreenshotPreloadOnTouchListener);
+ mNavigationBarView.gethideButton().setOnClickListener(mhideButton);
mNavigationBarView.getRecentsButton().setOnClickListener(mRecentsClickListener);
mNavigationBarView.getRecentsButton().setOnTouchListener(mRecentsPreloadOnTouchListener);
到这里的话,功能基本上就实现了,其实逻辑上是很简单的。
由于是第一次在CSDN上写文章,有哪里不对之处,还请大家帮忙指出。谢谢。