需求说明

导航栏增加按键

平台:

rk3399 + Android 7.1


实现

1.导航栏增加按键

frameworks/base/packages/SystemUI/res/drawable-nodpi/ic_sysbar_back.pngframeworks/base/packages/SystemUI/res/values/strings.xml添加strings,xml字符串Power

  2.增加按键layout.


|-- frameworks/base/packages/SystemUI/res/layout/power.xml

其中, keyCode是按键值, 若不想处理为按键, 则置为0

 3.在NavigationBarView中添加Layout

 (1)添加导航栏按键集合

|-- frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java

    public NavigationBarView(Context context, AttributeSet attrs) {        super(context, attrs);        mDisplay = ((WindowManager) context.getSystemService(                Context.WINDOW_SERVICE)).getDefaultDisplay();        mVertical = false;        mShowMenu = false;        mGestureHelper = new NavigationBarGestureHelper(context);        mConfiguration = new Configuration();        mConfiguration.updateFrom(context.getResources().getConfiguration());        updateIcons(context, Configuration.EMPTY, mConfiguration);        mBarTransitions = new NavigationBarTransitions(this);        mButtonDisatchers.put(R.id.back, new ButtonDispatcher(R.id.back));        mButtonDisatchers.put(R.id.home, new ButtonDispatcher(R.id.home));        mButtonDisatchers.put(R.id.recent_apps, new ButtonDispatcher(R.id.recent_apps));        mButtonDisatchers.put(R.id.menu, new ButtonDispatcher(R.id.menu));        mButtonDisatchers.put(R.id.ime_switcher, new ButtonDispatcher(R.id.ime_switcher));        mButtonDisatchers.put(R.id.screenshot, new ButtonDispatcher(R.id.screenshot));        mButtonDisatchers.put(R.id.volume_add, new ButtonDispatcher(R.id.volume_add));        mButtonDisatchers.put(R.id.volume_sub, new ButtonDispatcher(R.id.volume_sub));             //添加到集合.        mButtonDisatchers.put(R.id.power, new ButtonDispatcher(R.id.power));    }//供外部调用    public ButtonDispatcher getPowerButton() {        return mButtonDisatchers.get(R.id.power);    }

 

(2)默认按键布局config_navBarLayout定义:

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java:        return mContext.getString(R.string.config_navBarLayout);frameworks/base/packages/SystemUI/src/com/android/systemui/tuner/NavBarTuner.java:            navLayout = context.getString(R.string.config_navBarLayout);frameworks/base/packages/SystemUI/res/values/config.xml:    space;volume_sub,back,home,recent,volume_add,screenshot;menu_imeframeworks/base/packages/SystemUI/res/values-sw600dp/config.xml:    space;volume_sub,back,home,recent,volume_add,screenshot;menu_imeframeworks/base/packages/SystemUI/res/values-sw900dp/config.xml:    space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime

   增加导航栏个数:

 

diff --git a/frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml b/frameworks/base/packages/SystemUI/res/values-sw600dp/config.xmlold mode 100644new mode 100755index aa03ab2..ef41fee--- a/frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml+++ b/frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml@@ -34,7 +34,7 @@     true      -    space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime+    space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime           290diff --git a/frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml b/frameworks/base/packages/SystemUI/res/values-sw900dp/config.xmlold mode 100644new mode 100755index 016f7e5..c992349--- a/frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml+++ b/frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml@@ -19,6 +19,6 @@       -    space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime+    space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime  diff --git a/frameworks/base/packages/SystemUI/res/values/config.xml b/frameworks/base/packages/SystemUI/res/values/config.xmlold mode 100644new mode 100755index da5f4bf..59e7161--- a/frameworks/base/packages/SystemUI/res/values/config.xml+++ b/frameworks/base/packages/SystemUI/res/values/config.xml@@ -280,7 +280,7 @@     com.android.systemui.SystemUIFactory      -    space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime+    space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime      false

 

(3)加载Layout:

      |-- frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java

    

public static final String POWER = "power";//加载按键    @Override    protected void onFinishInflate() {        super.onFinishInflate();        inflateChildren();        clearViews();        inflateLayout(getDefaultLayout());    }//默认按键    protected String getDefaultLayout() {        return mContext.getString(R.string.config_navBarLayout);    }    protected void inflateLayout(String newLayout) {        mCurrentLayout = newLayout;        if (newLayout == null) {            newLayout = getDefaultLayout();        }        String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);        String[] start = sets[0].split(BUTTON_SEPARATOR);        String[] center = sets[1].split(BUTTON_SEPARATOR);        String[] end = sets[2].split(BUTTON_SEPARATOR);        // Inflate these in start to end order or accessibility traversal will be messed up.        inflateButtons(start, (ViewGroup) mRot0.findViewById(R.id.ends_group), isRot0Landscape);        inflateButtons(start, (ViewGroup) mRot90.findViewById(R.id.ends_group), !isRot0Landscape);        LinearLayout centerLayout = (LinearLayout)mRot0.findViewById(R.id.center_group);        mIsReverseInflateRot0 = !isRot0Landscape && !isSw600Dp() && mCurrentOrientation == Configuration.ORIENTATION_LANDSCAPE;        if(mIsReverseInflateRot0)            centerLayout.setOrientation(LinearLayout.VERTICAL);        inflateButtons(center, centerLayout, isRot0Landscape);        mIsReverseInflateRot0 = false;        centerLayout = (LinearLayout)mRot90.findViewById(R.id.center_group);        mIsReverseInflateRot90 = !isRot0Landscape && !isSw600Dp() && mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT;        if(mIsReverseInflateRot90)            centerLayout.setOrientation(LinearLayout.HORIZONTAL);        inflateButtons(center, centerLayout, !isRot0Landscape);        mIsReverseInflateRot90 = false;        addGravitySpacer((LinearLayout) mRot0.findViewById(R.id.ends_group));        addGravitySpacer((LinearLayout) mRot90.findViewById(R.id.ends_group));        inflateButtons(end, (ViewGroup) mRot0.findViewById(R.id.ends_group), isRot0Landscape);        inflateButtons(end, (ViewGroup) mRot90.findViewById(R.id.ends_group), !isRot0Landscape);    }//加入导航栏    @Nullable    protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape) {        LayoutInflater inflater = landscape ? mLandscapeInflater : mLayoutInflater;        float size = extractSize(buttonSpec);        String button = extractButton(buttonSpec);        View v = null;        if (HOME.equals(button)) {            v = inflater.inflate(R.layout.home, parent, false);            if (landscape && isSw600Dp()) {                setupLandButton(v);            }        } else if (BACK.equals(button)) {            v = inflater.inflate(R.layout.back, parent, false);            if (landscape && isSw600Dp()) {                setupLandButton(v);            }        } else if (RECENT.equals(button)) {            v = inflater.inflate(R.layout.recent_apps, parent, false);            if (landscape && isSw600Dp()) {                setupLandButton(v);            }        } else if (SCREENSHOT.equals(button)) {            v = inflater.inflate(R.layout.screenshot, parent, false);            if (landscape && isSw600Dp()) {                setupLandButton(v);            }//下面增加POWER按键, layout.power.xml也是在这里使用        }else if (POWER.equals(button)) {            v = inflater.inflate(R.layout.power, parent, false);            if (landscape && isSw600Dp()) {                setupLandButton(v);            }        } else if (VOLUME_ADD.equals(button)) {            v = inflater.inflate(R.layout.volume_add, parent, false);            if (landscape && isSw600Dp()) {                setupLandButton(v);            }        } else if (VOLUME_SUB.equals(button)) {            v = inflater.inflate(R.layout.volume_sub, parent, false);            if (landscape && isSw600Dp()) {                setupLandButton(v);            }        } else if (MENU_IME.equals(button)) {            v = inflater.inflate(R.layout.menu_ime, parent, false);        } else if (NAVSPACE.equals(button)) {            v = inflater.inflate(R.layout.nav_key_space, parent, false);        } else if (CLIPBOARD.equals(button)) {            v = inflater.inflate(R.layout.clipboard, parent, false);        } else if (button.startsWith(KEY)) {            String uri = extractImage(button);            int code = extractKeycode(button);            v = inflater.inflate(R.layout.custom_key, parent, false);            ((KeyButtonView) v).setCode(code);            if (uri != null) {                ((KeyButtonView) v).loadAsync(uri);            }        } else {            return null;        }        if (size != 0) {            ViewGroup.LayoutParams params = v.getLayoutParams();            params.width = (int) (params.width * size);        }        if(mIsReverseInflateRot0 || mIsReverseInflateRot90){            setupVerticalButton(v);        }        parent.addView(v);        addToDispatchers(v);        View lastView = landscape ? mLastLandscape : mLastPortrait;        if (lastView != null) {            v.setAccessibilityTraversalAfter(lastView.getId());        }        if (landscape) {            mLastLandscape = v;        } else {            mLastPortrait = v;        }        return v;    }

 

4.添加设置按键事件可见:

 |-- frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

//导入接口import android.widget.Toast;//点击处理:private View.OnClickListener mPowerClickListener = new View.OnClickListener(){public void onClick(View v){              Toast.makeText(mContext,"power power ",Toast.LENGTH_LONG).show();}};private void prepareNavigationBarView() {        mNavigationBarView.reorient();        ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();        recentsButton.setOnClickListener(mRecentsClickListener);        recentsButton.setOnTouchListener(mRecentsPreloadOnTouchListener);        recentsButton.setLongClickable(true);        recentsButton.setOnLongClickListener(this::handleLongPressBackRecents);        ButtonDispatcher backButton = mNavigationBarView.getBackButton();        backButton.setLongClickable(true);        backButton.setOnLongClickListener(this::handleLongPressBackRecents);        ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();        homeButton.setOnTouchListener(mHomeActionListener);        homeButton.setOnLongClickListener(mLongPressHomeListener);         ButtonDispatcher screenshotButton=mNavigationBarView.getScreenshotButton();        screenshotButton.setOnClickListener(mScreenshotClickListener);        screenshotButton.setOnTouchListener(mScreenshotTouchListener);        screenshotButton.setVisibility(View.VISIBLE);        boolean isShow=Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREENSHOT_BUTTON_SHOW, 1)==1;        if(isShow){            screenshotButton.setVisibility(View.VISIBLE);        }else{            screenshotButton.setVisibility(View.GONE);        }//添加监听并使用按键可见.ButtonDispatcher powerButton=mNavigationBarView.getPowerButton();        powerButton.setOnClickListener(mPowerClickListener);        powerButton.setVisibility(View.VISIBLE);}

 

 

 

更多相关文章

  1. android开发之onCreate( )方法详解
  2. 【Android】Spannable实现文字高亮
  3. Android视图加载流程(2)之Window和WindowManager的创建与Activit
  4. android 自定义View设置自定义监听 框架(监听自定义字符)
  5. android(5) 等待窗口
  6. Android(安卓)ListView下拉刷新点击加载更多[转]
  7. 演化理解 Android(安卓)异步加载图片
  8. Android(安卓)正则表达式 匹配 (数字)x(数字)
  9. Android(安卓)获取manifest.xml中meta-data值遇到的问题

随机推荐

  1. Android 讯飞语音之语音合成(在线有声朗读
  2. Android常用的两种基本布局——线性布局L
  3. Android中使用Parcelable
  4. 关于webview调用js
  5. Android模拟器常用使用,和基本功能使用
  6. Android(安卓)View 绘制过程
  7. Android可拖拽布局ConstraintLayout
  8. (一)Android官方MVVM框架实现组件化之整
  9. Android(安卓)NDK开发之旅28--C++--vecto
  10. android 自定义Adapter的心得