当前位置:网站首页>systemui状态栏添加新图标

systemui状态栏添加新图标

2022-08-10 14:29:00 纵容_伊人倩影

状态栏右侧添加新图标
需求描述

在状态栏右边添加一个自定义图标,根据广播更新图标。

代码实现

framework部分修改
frameworks/base/core/res/res/values/config.xml最上方config_statusBarIcons。原生注释写的很清楚了,用不要钱的翻译翻译一下:
不要翻译。
定义右侧图标的插槽。
也就是状态栏里不是通知的图标
1、config_statusBarIcons数组里添加自定义图标xiguatong
2、在下方添加string xiguatong
frameworks/base/core/res/res/values/config.xml中config_statusBarIcons的顺序表示右侧状态栏图标的顺序,我们在电量图标前面加了一个xiguatong

<!-- Do not translate. Defines the slots for the right-hand side icons. That is to say, the icons in the status bar that are not notifications. -->
    <string-array name="config_statusBarIcons">
        <item><xliff:g id="id">@string/status_bar_alarm_clock</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_rotate</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_headset</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_data_saver</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_ime</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_sync_failing</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_sync_active</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_nfc</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_tty</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_speakerphone</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_cdma_eri</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_data_connection</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_phone_evdo_signal</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_phone_signal</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_secure</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_bluetooth</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_managed_profile</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_cast</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_screen_record</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_vpn</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_camera</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_microphone</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_location</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_mute</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_volume</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_zen</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_ethernet</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_wifi</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_hotspot</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_mobile</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_airplane</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_xiguatong</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_battery</xliff:g></item>
        <item><xliff:g id="id">@string/status_bar_sensors_off</xliff:g></item>
    </string-array>
	<string translatable="false" name="status_bar_xiguatong">xiguatong</string>
    <string translatable="false" name="status_bar_rotate">rotate</string>
    <string translatable="false" name="status_bar_headset">headset</string>
    <string translatable="false" name="status_bar_data_saver">data_saver</string>
    <string translatable="false" name="status_bar_managed_profile">managed_profile</string>
    <string translatable="false" name="status_bar_ime">ime</string>
    <string translatable="false" name="status_bar_sync_failing">sync_failing</string>
    <string translatable="false" name="status_bar_sync_active">sync_active</string>
    <string translatable="false" name="status_bar_cast">cast</string>
    <string translatable="false" name="status_bar_hotspot">hotspot</string>
    <string translatable="false" name="status_bar_location">location</string>
    <string translatable="false" name="status_bar_bluetooth">bluetooth</string>
    <string translatable="false" name="status_bar_nfc">nfc</string>
    <string translatable="false" name="status_bar_tty">tty</string>
    <string translatable="false" name="status_bar_speakerphone">speakerphone</string>
    <string translatable="false" name="status_bar_zen">zen</string>
    <string translatable="false" name="status_bar_mute">mute</string>
    <string translatable="false" name="status_bar_volume">volume</string>
    <string translatable="false" name="status_bar_wifi">wifi</string>
    <string translatable="false" name="status_bar_cdma_eri">cdma_eri</string>
    <string translatable="false" name="status_bar_data_connection">data_connection</string>
    <string translatable="false" name="status_bar_phone_evdo_signal">phone_evdo_signal</string>
    <string translatable="false" name="status_bar_phone_signal">phone_signal</string>
    <string translatable="false" name="status_bar_battery">battery</string>
    <string translatable="false" name="status_bar_alarm_clock">alarm_clock</string>
    <string translatable="false" name="status_bar_secure">secure</string>
    <string translatable="false" name="status_bar_clock">clock</string>
    <string translatable="false" name="status_bar_mobile">mobile</string>
    <string translatable="false" name="status_bar_vpn">vpn</string>
    <string translatable="false" name="status_bar_ethernet">ethernet</string>
    <string translatable="false" name="status_bar_microphone">microphone</string>
    <string translatable="false" name="status_bar_camera">camera</string>
    <string translatable="false" name="status_bar_airplane">airplane</string>
    <string translatable="false" name="status_bar_sensors_off">sensors_off</string>
    <string translatable="false" name="status_bar_screen_record">screen_record</string>

frameworks/base/core/res/res/values/symbols.xml中添加xiguatong id,不然引用不到资源

  <java-symbol type="string" name="status_bar_xiguatong" />
  <java-symbol type="string" name="status_bar_battery" />

systemui部分
vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java

private final StatusBarIconController mIconController;//状态栏图标控制器
//自己加的西瓜桶图标
private final String mXiGuaTong;
private boolean mXiGuaTongVisible;
@Inject
public PhoneStatusBarPolicy(...){
    
	...
	//
	mXiGuaTong = resources.getString(com.android.internal.R.string.status_bar_xiguatong);
	mXiGuaTongVisible = true;
	...
}
public void init() {
    
	...
	//xiGuaTong:字符串,图片,按钮描述
	mIconController.setIcon(mXiGuaTong, R.drawable.stat_sys_xiguatong,null);
	//设置图片可见不可见:字符串,Boolean值
	mIconController.setIconVisibility(mXiGuaTong, mXiGuaTongVisible);
	...
}

以上,就可以在状态栏右侧添加新图标。

遗留问题

1、图标是怎么加到状态栏右边的,完整流程?
2、状态栏左边怎么加图标?
3、状态栏、下拉状态栏、2次下拉状态栏、锁屏、这几种状态下状态栏图标会有变化,是哪里控制的?

原网站

版权声明
本文为[纵容_伊人倩影]所创,转载请带上原文链接,感谢
https://blog.csdn.net/a396604593/article/details/124193812