欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 7.1 导航栏增加按键

程序员文章站 2022-08-06 20:48:08
需求说明导航栏增加按键平台:rk3399+ Android 7.1实现导航栏增加按键frameworks/base/packages/SystemUI/res/drawable-nodpi/ic_sysbar_back.pngframeworks/base/packages/SystemUI/res/values/strings.xml添加strings,xml字符串

需求说明

导航栏增加按键

平台:

rk3399 + Android 7.1


实现

1.导航栏增加按键

frameworks/base/packages/SystemUI/res/drawable-nodpi/ic_sysbar_back.png
frameworks/base/packages/SystemUI/res/values/strings.xml

添加strings,xml字符串
<string name="accessibility_power" translatable="false">Power</string>


  2.增加按键layout.


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

<com.android.systemui.statusbar.policy.KeyButtonView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:id="@+id/power"
    android:layout_width="@dimen/navigation_key_width"
    android:layout_height="match_parent"
    android:layout_weight="0"
    android:src="@drawable/ic_sysbar_back"
    systemui:keyCode="0"
    android:scaleType="center"
    android:contentDescription="@string/accessibility_power"
    android:paddingStart="@dimen/navigation_key_padding"
    android:paddingEnd="@dimen/navigation_key_padding"
    />

其中, 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:    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime</string>
	frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml:    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime</string>
	frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml:    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime</string>

   增加导航栏个数:

 

diff --git a/frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml b/frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml
old mode 100644
new mode 100755
index 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 @@
     <bool name="config_keyguardUserSwitcher">true</bool>
 
     <!-- Nav bar button default ordering/layout -->
-    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime</string>
+    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime</string>
 
     <!-- Animation duration when using long press on recents to dock -->
     <integer name="long_press_dock_anim_duration">290</integer>
diff --git a/frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml b/frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml
old mode 100644
new mode 100755
index 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 @@
 <resources>
 
     <!-- Nav bar button default ordering/layout -->
-    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime</string>
+    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime</string>
 
 </resources>
diff --git a/frameworks/base/packages/SystemUI/res/values/config.xml b/frameworks/base/packages/SystemUI/res/values/config.xml
old mode 100644
new mode 100755
index da5f4bf..59e7161
--- a/frameworks/base/packages/SystemUI/res/values/config.xml
+++ b/frameworks/base/packages/SystemUI/res/values/config.xml
@@ -280,7 +280,7 @@
     <string name="config_systemUIFactoryComponent" translatable="false">com.android.systemui.SystemUIFactory</string>
 
     <!-- Nav bar button default ordering/layout -->
-    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot;menu_ime</string>
+    <string name="config_navBarLayout" translatable="false">space;volume_sub,back,home,recent,volume_add,screenshot,power;menu_ime</string>
 
     <bool name="quick_settings_show_full_alarm">false</bool>

 

(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);
		
	}

 

 

 

本文地址:https://blog.csdn.net/qq_32014215/article/details/107557201