首页 > 其他分享 >Android 12 自定义底部导航栏

Android 12 自定义底部导航栏

时间:2023-06-19 09:22:36浏览次数:68  
标签:12 自定义 button System getDrawable id Android else drawable

1.修改配置文件 frameworks\base\packages\SystemUI\res\values\config.xml

<!-- Nav bar button default ordering/layout -->
    <string name="config_navBarLayout" translatable="false">left[.5W];leftrotate,volume_sub,back,home,recent,volume_add,screenshot,rightrotate,setting,hidenav;right[.5W]</string>
    <string name="config_navBarLayoutQuickstep" translatable="false">back[1.7WC];home;contextual[1.7WC]</string>
    <string name="config_navBarLayoutHandle" translatable="false">back[70AC];home_handle;ime_switcher[70AC]</string>

添加自己新增加的button名称 

同步修改 frameworks\base\packages\SystemUI\res\values-sw600dp\config.xml   frameworks\base\packages\SystemUI\res\values-sw900dp\config.xml文件夹

sw是smallwidth的意思,当屏幕的最小边像素大于900 就会使用values-sw900dp\config.xml 

2.为新添加的button 创建layout布局 放在layout文件夹下 

<com.android.systemui.navigationbar.buttons.KeyButtonView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:id="@+id/hidenav"  button id
    android:layout_width="@dimen/navigation_key_width"
    android:layout_height="match_parent"
    android:layout_weight="0"
    systemui:keyCode="29"  很重要 指定keyCode 用于响应点击事件
    android:scaleType="center"
    android:paddingStart="@dimen/navigation_key_padding"
    android:paddingEnd="@dimen/navigation_key_padding"
    />

3.修改SystemUI Java源码 

frameworks\base\packages\SystemUI\src\com\android\systemui\navigationbar\NavigationBarInflaterView.java

在 createView里 加载button对应的layout文件 

else if (SETTING.equals(button)) {
            v = inflater.inflate(R.layout.setting, parent, false);
        }else if (HIDE.equals(button)) {
            v = inflater.inflate(R.layout.hidenav, parent, false);
        }

frameworks\base\packages\SystemUI\src\com\android\systemui\navigationbar\NavigationBarView.java

在构造方法中 添加button

mButtonDispatchers.put(R.id.left, new ButtonDispatcher(R.id.left));
mButtonDispatchers.put(R.id.right, new ButtonDispatcher(R.id.right));
mButtonDispatchers.put(R.id.setting, new ButtonDispatcher(R.id.setting));
mButtonDispatchers.put(R.id.hidenav, new ButtonDispatcher(R.id.hidenav));

定义新增加的Button

private KeyButtonDrawable mLeft;
private KeyButtonDrawable mRight;
private KeyButtonDrawable mSetting;
private KeyButtonDrawable mHide;

并在updateIcons方法中 为KeyButtonDrawable  指定 资源文件

    private void updateIcons(Configuration oldConfig) {
        final boolean orientationChange = oldConfig.orientation != mConfiguration.orientation;
        final boolean densityChange = oldConfig.densityDpi != mConfiguration.densityDpi;
        final boolean dirChange = oldConfig.getLayoutDirection() != mConfiguration.getLayoutDirection();

        if (orientationChange || densityChange) {
            mDockedIcon = getDrawable(R.drawable.ic_sysbar_docked);
            mHomeDefaultIcon = getHomeDrawable();
        }
        if (densityChange || dirChange) {
            mRecentIcon = getDrawable(R.drawable.ic_sysbar_recent);
            mContextualButtonGroup.updateIcons(mLightIconColor, mDarkIconColor);
        }
        if (orientationChange || densityChange || dirChange) {
            mBackIcon = getBackDrawable();
        }

        mVolumeAddIcon = getDrawable(R.drawable.ic_sysbar_volume_add_button);
        mVolumeSubIcon = getDrawable(R.drawable.ic_sysbar_volume_sub_button);
        mScreenshotIcon = getDrawable(R.drawable.ic_sysbar_capture_button);
        mLeft = getDrawable(R.drawable.leftrotate);
        mRight = getDrawable(R.drawable.rightrotate);
        mSetting = getDrawable(R.drawable.settings);
        mHide = getDrawable(R.drawable.hide);
    }

创建 获取ButtonDispatcher 的方法

public ButtonDispatcher getHideButton() {
        return mButtonDispatchers.get(R.id.hidenav);
    }

在updateNavButtonIcons方法中 为button指定resource 和可见性

getLeftButton().setImageDrawable(mLeft);
getRightButton().setImageDrawable(mRight);
getHideButton().setImageDrawable(mHide);
getSettingsButton().setImageDrawable(mSetting);

getLeftButton().setVisibility(View.VISIBLE);
getRightButton().setVisibility(View.VISIBLE);
getHideButton().setVisibility(View.VISIBLE);
getSettingsButton().setVisibility(View.VISIBLE);

4.在frameworks\base\packages\SystemUI\src\com\android\systemui\navigationbar\buttons\KeyButtonView.java为button 的onTouchEvent方法处理button的点击事件

     case MotionEvent.ACTION_UP:
                final boolean doIt = isPressed() && !mLongClicked;
                setPressed(false);
                final boolean doHapticFeedback = (SystemClock.uptimeMillis() - mDownTime) > 150;
                if (showSwipeUI) {
                    if (doIt) {
                        // Apply haptic feedback on touch up since there is none on touch down
                        performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
                        playSoundEffect(SoundEffectConstants.CLICK);
                    }
                } else if (doHapticFeedback && !mLongClicked) {
                    // Always send a release ourselves because it doesn't seem to be sent elsewhere
                    // and it feels weird to sometimes get a release haptic and other times not.
                    performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY_RELEASE);
                }
                if (mCode != KEYCODE_UNKNOWN) {
                    Log.e("longjiang","mCode"+mCode);
                    int rightCount = 0;
                    int leftCount = 0;
                    if(mCode == 26){
                        rightCount++;
                        Log.e("longjiang","rightCount"+rightCount);
                        Settings.System.putInt(mContext.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0);
                        Settings.System.putInt(mContext.getContentResolver(),Settings.System.USER_ROTATION, (4-rightCount%4)%4);
                    }else if(mCode == 25){
                        leftCount++;
                        Log.e("longjiang","leftCount"+leftCount);
                        Settings.System.putInt(mContext.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0);
                        Settings.System.putInt(mContext.getContentResolver(),Settings.System.USER_ROTATION, leftCount%4);
                    }else if(mCode ==  27){
                        //通过目标APP的包名创建intent对象
                        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage("com.android.settings");
                        // 这里如果intent为空,就说名没有安装要跳转的应用嘛
                        if (intent != null) {
                            //传参方式与Activity间跳转相同
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                        } else {
                            //没有安装目标app应用
                        }
                    }else if(mCode ==  29){
                        SystemProperties.set("vendor.hide.nav","true");
                        // View gameView = mContext.getWindow().getDecorView();
                        // gameView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); // hide nav bar
                        //gameView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE // show nav bar);
                    }
                    if (doIt) {
                        sendEvent(KeyEvent.ACTION_UP, 0);
                        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
                    } else {
                        sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED);
                    }
                } else {
                    // no key code, just a regular ImageView
                    if (doIt && mOnClickListener != null) {
                        mOnClickListener.onClick(this);
                        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
                    }
                }
                removeCallbacks(mCheckLongPress);
                break;
        }

 

   

标签:12,自定义,button,System,getDrawable,id,Android,else,drawable
From: https://www.cnblogs.com/wanglongjiang/p/17490269.html

相关文章

  • 自定义异常和统一校验参数
    自定义异常@GetterpublicclassBusinessExceptionextendsRuntimeException{/***http状态码*/privateintcode;privateObjectobject;publicBusinessException(Stringmessage,intcode,Objectobject){super(message);......
  • UVA12716 GCD等于XOR GCD XOR
    UVA12716GCD等于XORGCDXOR一道数学题。 首先,我们可以知道,a-b>=gcd(a,b)=c;其次,a-b<=axorb=c;综上,可得a-b=c,即a-b=axorb.由于范围不大,直接枚举。第一层枚举c(因为c较少),第二层枚举a,(b=a-c) 再判断c是否等于a^b即可。#include<bits/stdc++.h>usingnamespacestd;......
  • 浅谈生活中常见的三大应用程序架构(PE、ELF、Mach-O)、五大操作系统(windows、linux、mac
    ·今天不聊复杂的技术,就是想做一下科普。我们生活中常见的操作系统,大致有5种分别是 电脑: Windows linux    macos手机 androidiosWindows手机操作系统没有发展起来,不同的操作系统间软件不能......
  • 给Nexus6p刷入lineage14.1(android 7.1)和 nethunter 2019.3
    本文依据kali教程编写https://build.nethunter.com/contributors/re4son/angler/INSTALLATION.txt写在前面的话你可能很奇怪,为什么有kali2020.3不用要刷入2019.3版本的。其实目的是使用安卓7,因为高版本安卓对某些软件的兼容性太差,刷入2019载手动升级到2020.3.Andrax在安卓7、9......
  • Hexo + Butterfly 自定义页脚
    原文链接:Hexo+Butterfly自定义页脚推荐阅读基于Hexo从零开始搭建个人博客(一):环境准备基于Hexo从零开始搭建个人博客(二):项目初识基于Hexo从零开始搭建个人博客(三):主题安装基于Hexo从零开始搭建个人博客(四):基础配置基于Hexo从零开始搭建个人博客(五):详细......
  • 2023.6.18 12.数据库配置优化
    12.数据库配置优化mysql数据库优化框架体系优化要有框架和体系:根据⽤户访问⽹站流程优化集群,根据OSI7层模型,从下往上优化数据库!1、硬件层⾯优化1.1数据库物理机采购:1.2服务器硬件配置调整1.2.1服务器BIOS调整1.2.2阵列卡调整2、软件层优化2.1操作系统层⾯优化2.1.1操......
  • 在执行建表语句时提示:Row size too large (> 8126)
    在执行建表语句时提示:在mysqld节点下添加以下配置,然后重启mysql[mysqld]innodb_log_file_size=500Minnodb_log_buffer_size=800Minnodb_strict_mode=0 ......
  • 2712.minimum Cost to Make All Characters Equal
    Description2712.MinimumCosttoMakeAllCharactersEqual(Medium)Youaregivena0-indexedbinarystringsoflengthnonwhichyoucanapplytwotypesofoperations:Chooseanindexiandinvertallcharactersfromindex0toindexi(bothinclusive......
  • PAT Advanced 1012. The Best Rank
    PATAdvanced1012.TheBestRank1.ProblemDescription:ToevaluatetheperformanceofourfirstyearCSmajoredstudents,weconsidertheirgradesofthreecoursesonly:C-CProgrammingLanguage,M-Mathematics(CalculusorLinearAlgrbra),andE-E......
  • backtrader 自定义分析器,解决多股回测难分析困难问题
    backtrader自定义分析器,解决多股回测分析困难问题解决了啥:解决回测后获取关键指标解决多股回测,获取订单分析解决多股回测买卖点可视化标识效果图通过自定义分析器KeyIndicatorAnalyzer,TradeListAnalyzer,获取回测结果数据,通过回测数据可以轻松可视化回测结果。可视化部......