Android 8.1平台客制化虚拟导航按键


需求

基于MTK8163 8.1平台定制导航栏部分,在左边增加音量减,右边增加音量加

运行截图

见图

代码修改步骤

  • 增加需要的音量资源文件,增加4张图片到drawable图片资源下,注意尺寸和back,home,recent一致,分为亮色和暗色两种图片。(图片尽然还要自己搞,幸好有在线PS可以修改尺寸和颜色) ic_sysbar_volume_up.png,ic_sysbar_volume_up_dark.png,ic_sysbar_volume_down.png,ic_sysbar_volume_down_dark.png;

  • layout下增加volume_down.xml 和 volume_up.xml(里面keyRepeatExt是自己定义的,可以忽略,为了标识是音量键,方便在KeyButtonView.java里作特殊长按处理)

"http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:id="@+id/volume_down"
    android:layout_width="@dimen/navigation_key_width"
    android:layout_height="match_parent"
    android:layout_weight="0"
    systemui:keyCode="25"
    systemui:keyRepeatExt="true"
    android:scaleType="center"
    android:contentDescription="@string/accessibility_key"
    android:paddingStart="@dimen/navigation_key_padding"
    android:paddingEnd="@dimen/navigation_key_padding"
    />

"http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:id="@+id/volume_up"
    android:layout_width="@dimen/navigation_key_width"
    android:layout_height="match_parent"
    android:layout_weight="0"
    systemui:keyCode="24"
    systemui:keyRepeatExt="true" 
    android:scaleType="center"
    android:contentDescription="@string/accessibility_key"
    android:paddingStart="@dimen/navigation_key_padding"
    android:paddingEnd="@dimen/navigation_key_padding"
    />
复制代码
  • 修改res/values-sw600dp/config.xml下的name为config_navBarLayout配置,增加音量加减选项(我这里平板,对values-sw600dp生效,默认是values)
 "config_navBarLayout" translatable="false">left;volume_down,back,home,recent,volume_up;right
复制代码
  • 修改res/values-sw600dp/dimens.xml下的navigation_key_width和navigation_key_padding参数,对每个icon调整合适的宽度,以适应增加音量加减之后的布局
"navigation_key_width">90dp
"navigation_key_padding">0dp
复制代码
  • NavigationBarInflaterView.java中,增加
public static final String VOLUME_DOWN = "volume_down";
public static final String VOLUME_UP = "volume_up";
复制代码

createView()方法里增加 ,以加载音量加减的布局:

 else if (VOLUME_DOWN.equals(button)) {
            v = inflater.inflate(R.layout.volume_down, parent, false);

 } else if (VOLUME_UP.equals(button)) {
            v = inflater.inflate(R.layout.volume_up, parent, false);
 }
复制代码
  • NavigationBarView.java里,增加:
private KeyButtonDrawable mVolumeDown,mVolumeUp;
复制代码

构造方法里增加,put到mButtonDispatchers数据结构里:

mButtonDispatchers.put(R.id.volume_down, new ButtonDispatcher(R.id.volume_down));
mButtonDispatchers.put(R.id.volume_up, new ButtonDispatcher(R.id.volume_up));
getVolumeDownButton().setLongClickable(false);
getVolumeUpButton().setLongClickable(false);
复制代码

增加新方法:

public ButtonDispatcher getVolumeDownButton() {
        return mButtonDispatchers.get(R.id.volume_down);
}

public ButtonDispatcher getVolumeUpButton() {
        return mButtonDispatchers.get(R.id.volume_up);
}

复制代码

updateIcons()方法里增加,获取音量加减的icon资源:

mVolumeDown = getDrawable(ctx,R.drawable.ic_sysbar_volume_down,R.drawable.ic_sysbar_volume_down_dark);
mVolumeUp = getDrawable(ctx,R.drawable.ic_sysbar_volume_up,R.drawable.ic_sysbar_volume_up_dark);
复制代码

调用方法setNavigationIconHints()里增加对音量图片资源的设置:

getVolumeDownButton().setImageDrawable(mVolumeDown);
getVolumeUpButton().setImageDrawable(mVolumeUp);
复制代码

setDisabledFlags()方法里增加:

 //volume down and up just follow the home key
getVolumeDownButton().setVisibility(disableHome ? View.INVISIBLE : View.VISIBLE);
getVolumeUpButton().setVisibility(disableHome ? View.INVISIBLE : View.VISIBLE);
复制代码

至此,make,push...

转载于:https://juejin.im/post/5ad9a5326fb9a07aa83e49be

你可能感兴趣的:(Android 8.1平台客制化虚拟导航按键)