经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
对Activity的DecorView的包装(二)
来源:cnblogs  作者:luo0612  时间:2018/10/9 10:03:28  对本文有异议


看了下公司的系统代码对于根布局decor_layout.xml的修改, 有所获.

前些时候才开始做系统开发的时候, 总想改改系统的源码, 至于原因: 人总是想装装, 在踩过几个别人修改的坑后, 还是觉得在不改源码的基础上, 尽量纳源码为自己所用还是挺好的.

代码如下:

  1. 1 public void wrapDecor(Activity activity) {
  2. 2
  3. 3 mWindow = activity.getWindow();
  4. 4 if(mWindow == null){
  5. 5 Log.e(TAG, "Window is null");
  6. 6 return;
  7. 7 }
  8. 8
  9. 9 View decorView = mWindow.getDecorView();
  10. 10 if (decorView == null) {
  11. 11 Log.e(TAG, "DecorView is null");
  12. 12 return;
  13. 13 }
  14. 14
  15. 15 ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);
  16. 16 if (contentView == null) {
  17. 17 Log.e(TAG, "DecorView is null, have you called wrapDecor after Activity#super.onCreate?");
  18. 18 return;
  19. 19 }
  20. 20
  21. 21 final int childCount = contentView.getChildCount();
  22. 22 if (childCount == 0) {
  23. 23 // Maybe called before Activity#setContentView
  24. 24 mPotentialErrorFlag |= FLAG_POTENTIAL_ERROR_SET_CONTENT;
  25. 25 }
  26. 26
  27. 27 View[] children = new View[childCount];
  28. 28 for (int i = 0; i < childCount; i++) {
  29. 29 children[i] = contentView.getChildAt(i);
  30. 30 }
  31. 31
  32. 32 contentView.removeAllViews();
  33. 33
  34. 34 LayoutInflater inflater = LayoutInflater.from(activity);
  35. 35
  36. 36 //===================== begin ========================
  37. 37
  38. 38 // 此处即为自定义的decor_layout.xml文件
  39. 39 View wrapper = inflater.inflate(R.layout.decor_layout, null);
  40. 40
  41. 41 ViewGroup rawContentView = (ViewGroup) wrapper.findViewById(R.id.content);
  42. 42 if (childCount > 0) {
  43. 43 for (View child : children) {
  44. 44 rawContentView.addView(child);
  45. 45 }
  46. 46 }
  47. 47 //change for listActivity, add view first then setContenView
  48. 48 activity.setContentView(wrapper);
  49. 49
  50. 50 //===================== end =======================
  51. 51
  52. 52 // 获取自定义decor_layout中的控件
  53. 53 mOptionsKey = wrapper.findViewById(R.id.feature_bar_options);
  54. 54
  55. 55 // 此处获取的是ActionBar的控件, 由于项目中需要大量使用到ActionBar,
  56. 56 // 此处对覆盖ActionBar对OptionMenu的控制
  57. 57 ActionBarView actionBarView = (ActionBarView) decorView.findViewById(
  58. 58 com.android.internal.R.id.action_bar);
  59. 59 if (actionBarView != null) {
  60. 60 // 覆盖ActionBar对OptionMenu的控制
  61. 61 actionBarView.setOverrideOverflowButton(mOptionsKey);
  62. 62 } else {
  63. 63 Log.d(TAG, "actionBarView is null");
  64. 64 if (mWindow != null) {
  65. 65 Log.d(TAG, "Attempt to invoke setShouldOverrideResources access PhoneWindow");
  66. 66 mWindow.setShouldOverrideResources(true);
  67. 67 } else {
  68. 68 Log.d(TAG, "mWindow is empty, pls check it");
  69. 69 }
  70. 70 }
  71. 71 }

该段代码的核心, 就在上面的 begin 和 end 之间, 代码挺简单, 使用到包装的思想, 也就是包装设计模式.

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号