需求
基於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裡作特殊長按處理)
<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/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"
/>
<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/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)
<string name="config_navBarLayout" translatable="false">left;volume_down,back,home,recent,volume_up;right</string>
複製程式碼
- 修改res/values-sw600dp/dimens.xml下的navigation_key_width和navigation_key_padding引數,對每個icon調整合適的寬度,以適應增加音量加減之後的佈局
<dimen name="navigation_key_width">90dp</dimen>
<dimen name="navigation_key_padding">0dp</dimen>
複製程式碼
- 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…