操作
com.android.systemui.statusbar.phone/StatusBarIconController.java类中修改如下方法:
@VisibleForTesting
protected StatusBarIconView addIcon(int index, String slot, boolean blocked,
StatusBarIcon icon) {
StatusBarIconView view = onCreateStatusBarIconView(slot, blocked);
view.set(icon);
//add:状态栏图标添加间距.
view.setPadding(2,0,2,0);
//end add.
mGroup.addView(view, index, onCreateLayoutParams());
return view;
}
@VisibleForTesting
protected StatusBarWifiView addSignalIcon(int index, String slot, WifiIconState state) {
StatusBarWifiView view = onCreateStatusBarWifiView(slot);
view.applyWifiState(state);
//add:状态栏图标添加间距.
view.setPadding(2,0,2,0);
//end add.
mGroup.addView(view, index, onCreateLayoutParams());
if (mIsInDemoMode) {
mDemoStatusIcons.addDemoWifiView(state);
}
return view;
}
@VisibleForTesting
protected StatusBarMobileView addMobileIcon(int index, String slot, MobileIconState state) {
StatusBarMobileView view = onCreateStatusBarMobileView(slot);
view.applyMobileState(state);
//add:状态栏图标添加间距.
view.setPadding(2,0,2,0);
//end add.
mGroup.addView(view, index, onCreateLayoutParams());
if (mIsInDemoMode) {
mDemoStatusIcons.addMobileView(state);
}
return view;
}
原理
状态栏添加图标最终会调用到
com.android.systemui.statusbar.phone/StatusBarIconController.java类中的addHolder()方法:
protected StatusIconDisplayable addHolder(int index, String slot, boolean blocked,
StatusBarIconHolder holder) {
switch (holder.getType()) {
case TYPE_ICON:
return addIcon(index, slot, blocked, holder.getIcon());
case TYPE_WIFI:
return addSignalIcon(index, slot, holder.getWifiState());
case TYPE_MOBILE:
return addMobileIcon(index, slot, holder.getMobileState());
}
return null;
}
就是在这里将图标添加到@+id/statusIcons中的
布局文件status_bar.xml
...
<com.android.keyguard.AlphaOptimizedLinearLayout android:id="@+id/system_icon_area"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="horizontal"
android:gravity="center_vertical|end"
>
<include layout="@layout/system_icons" />
</com.android.keyguard.AlphaOptimizedLinearLayout>
...
system_icon.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/system_icons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<com.android.systemui.statusbar.phone.StatusIconContainer android:id="@+id/statusIcons"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:paddingEnd="@dimen/signal_cluster_battery_padding"
android:gravity="center_vertical"
android:orientation="horizontal"/>
<com.android.systemui.BatteryMeterView android:id="@+id/battery"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:clipToPadding="false"
android:clipChildren="false" />
</LinearLayout>
然后进入addIcon方法:
@VisibleForTesting
protected StatusBarIconView addIcon(int index, String slot, boolean blocked,
StatusBarIcon icon) {
StatusBarIconView view = onCreateStatusBarIconView(slot, blocked);
view.set(icon);
mGroup.addView(view, index, onCreateLayoutParams());
return view;
}
addView的时候创建了onCreateLayoutParams()
public static class DarkIconManager extends IconManager {
...
private int mIconHPadding;
public DarkIconManager(LinearLayout linearLayout) {
super(linearLayout);
mIconHPadding = mContext.getResources().getDimensionPixelSize(
R.dimen.status_bar_icon_padding);
...
}
...
@Override
protected LayoutParams onCreateLayoutParams() {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, mIconSize);
lp.setMargins(mIconHPadding, 0, mIconHPadding, 0);
return lp;
}
}
这里可以设置一个margin,原生设置的0。
<!-- gap on either side of status bar notification icons -->
<dimen name="status_bar_icon_padding">0dp</dimen>
注意坑在这里,不管这里的margin值设为多少,最后并不会生效,因为在StatusIconContainer.java类中onLayout()方法中并没有考虑这个margin
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
float midY = getHeight() / 2.0f;
// Layout all child views so that we can move them around later
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
int top = (int) (midY - height / 2.0f);
child.layout(0, top, width, top + height);
}
resetViewStates();
calculateIconTranslations();
applyIconStates();
}
既然添加margin不行,那就添加padding,如文章开头的代码所示。