操作

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

...                    ...

system_icon.xml:

        

然后进入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。

    0dp

注意坑在这里,不管这里的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,如文章开头的代码所示。

更多相关文章

  1. android 获取锁屏,解锁的方法
  2. android中的activity里获得context方法
  3. android paint设置字体 中文字体 楷体 和自动换行方法(zhuan)
  4. Android发送短信,并监听短信发送后是否发送成功的实现方法
  5. 找不到R.java 解决方法
  6. Android Toast的几种简单调用方法
  7. Android使用SDK方法详解
  8. Android 发送普通的post请求的方法

随机推荐

  1. spring boot 获得 http请求url中的参数
  2. 龙门阵之 DevOps 门外汉须知
  3. 不得不会的Flink Dataset的DeltaI 迭代操
  4. 警告:小心被假持续集成骗了
  5. 浅谈程序员的职业发展
  6. 秘籍:微服务设计的六脉神剑
  7. Flink特异的迭代操作-bulkIteration
  8. TCP/IP之TCP报文简介
  9. 如何使用dhtmlxSuite构建的3个JavaScript
  10. BAT iOS算法面试题(汇总)