Android 2.3状态栏中添加menu home back快捷键

  • 格式:doc
  • 大小:157.50 KB
  • 文档页数:11

Android 2.3状态栏中添加menu home back快捷键在Android 2.3状态栏中添加menu,home和back快捷键的方法1、准备资源,修改XML文准备几张图,这里我们准备添加home back和menu图标,就需要准备6张图,三张普通状态,三张按下的高亮状态图标:stat_home.pngstat_home_pressed.pngstat_back.pngstat_back_pressed.pngstat_menu.pngstat_menu_pressed.png把它们放在frameworks/base/packages/SystemUI/res/drawable/目录下同时,在frameworks/base/packages/SystemUI/res/drawable 下创建三个imageButton的xml文件xml_stat_home.xml1. <?xml version="1.0"encoding="utf-8"?>2. <selector3. xmlns:android="/apk/res/android">4. <item5. android:state_focused="true"6. android:state_pressed="false"7. android:drawable="@drawable/stat_home"/>8. <item9. android:state_focused="true"10. android:state_pressed="true"11. android:drawable="@drawable/stat_home_pressed"/>12. <item13. android:state_focused="false"14. android:state_pressed="true"15. android:drawable="@drawable/stat_home_pressed"/>16. <item17. android:drawable="@drawable/stat_home"/>18. </selector>xml_stat_back.xml1. <?xml version="1.0"encoding="utf-8"?>2. <selector3. xmlns:android="/apk/res/android">4. <item5. android:state_focused="true"6. android:state_pressed="false"7. android:drawable="@drawable/stat_back"/>8. <item9. android:state_focused="true"10. android:state_pressed="true"11. android:drawable="@drawable/stat_back_pressed"/>12. <item13. android:state_focused="false"14. android:state_pressed="true"15. android:drawable="@drawable/stat_back_pressed"/>16. <item17. android:drawable="@drawable/stat_back"/>18. </selector>xml_stat_menu.xml1. <?xml version="1.0"encoding="utf-8"?>2. <selector3. xmlns:android="/apk/res/android">4. <item5. android:state_focused="true"6. android:state_pressed="false"7. android:drawable="@drawable/stat_menu"/>8. <item9. android:state_focused="true"10. android:state_pressed="true"11. android:drawable="@drawable/stat_menu_pressed"/>12. <item13. android:state_focused="false"14. android:state_pressed="true"15. android:drawable="@drawable/stat_menu_pressed"/>16. <item17. android:drawable="@drawable/stat_menu"/>18. </selector>修改status_bar.xml成如下目录:frameworks/base/packages/SystemUI/res/layout/status_bar.xml1. <?xml version="1.0" encoding="utf-8"?>2. <!--3. /* apps/common/assets/default/default/skins/StatusBar.xml4. **5. ** Copyright 2006, The Android Open Source Project6. **7. ** Licensed under the Apache License, Version 2.0 (the "License");8. ** you may not use this file except in compliance with the License.9. ** You may obtain a copy of the License at10. **11. ** /licenses/LICENSE-2.012. **13. ** Unless required by applicable law or agreed to in writing, software14. ** distributed under the License is distributed on an "AS IS" BASIS,15. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16. ** See the License for the specific language governing permissions and17. ** limitations under the License.18. */19. -->20.21. <!-- android:background="@drawable/status_bar_closed_default_background" -->22. <com.android.systemui.statusbar.StatusBarView23. xmlns:android="/apk/res/android"24. android:background="@drawable/statusbar_background"25. android:orientation="vertical"26. android:focusable="true"27. android:descendantFocusability="afterDescendants"28. >29.30. <LinearLayout android:id="@+id/icons"31. android:layout_width="match_parent"32. android:layout_height="match_parent"33. android:orientation="horizontal">34. <com.android.systemui.statusbar.IconMerger android:id="@+id/notificationIcons"35. android:layout_width="0dip"36. android:layout_weight="1"37. android:layout_height="match_parent"38. android:layout_alignParentLeft="true"39. android:paddingLeft="6dip"40. android:gravity="center_vertical"41. android:orientation="horizontal"/>42.43. <LinearLayout android:id="@+id/statusIcons"44. android:layout_width="wrap_content"45. android:layout_height="match_parent"46. android:layout_alignParentRight="true"47. android:paddingRight="6dip"48. android:gravity="center_vertical"49. android:orientation="horizontal"/>50. <ImageButton android:id="@+id/go_home"51. android:layout_width="32px"52. android:layout_height="32px"53. android:layout_alignParentLeft="true"54. android:paddingLeft="10dip"55. android:paddingTop="10dip"56. android:gravity="center_vertical"57. android:clickable="true"58. android:background="@drawable/xml_stat_home"59. />60. <ImageButton android:id="@+id/pop_menu"61. android:layout_width="32px"62. android:layout_height="32px"63. android:layout_alignParentRight="true"64. android:paddingLeft="10dip"65. android:paddingTop="10dip"66. android:gravity="center_vertical"67. android:orientation="horizontal"68. android:clickable="true"69. android:background="@drawable/xml_stat_menu"70. />71. <ImageButton android:id="@+id/go_back"72. android:layout_width="32px"73. android:layout_height="32px"74. android:layout_alignParentRight="true"75. android:paddingLeft="10dip"76. android:paddingTop="10dip"77. android:gravity="center_vertical"78. android:orientation="horizontal"79. android:clickable="true"80. android:background="@drawable/xml_stat_back"81. />82. <com.android.systemui.statusbar.Clock83. android:textAppearance="@*android:style/TextAppearance.StatusBar.Icon"84. android:layout_width="wrap_content"85. android:layout_height="match_parent"86. android:singleLine="true"87. android:paddingRight="6dip"88. android:gravity="center_vertical|left"89. />90. </LinearLayout>91.92. <LinearLayout android:id="@+id/ticker"93. android:layout_width="match_parent"94. android:layout_height="match_parent"95. android:paddingLeft="6dip"96. android:animationCache="false"97. android:orientation="horizontal" >98. <ImageSwitcher android:id="@+id/tickerIcon"99. android:layout_width="wrap_content"100. android:layout_height="match_parent"101. android:layout_marginRight="8dip"102. >103. <com.android.systemui.statusbar.AnimatedImageView104. android:layout_width="25dip"105. android:layout_height="25dip"106. />107. <com.android.systemui.statusbar.AnimatedImageView108. android:layout_width="25dip"109. android:layout_height="25dip"110. />111. </ImageSwitcher>112. <com.android.systemui.statusbar.TickerView android:id="@+id/tickerText"113. android:layout_width="0dip"114. android:layout_weight="1"115. android:layout_height="wrap_content"116. android:paddingTop="2dip"117. android:paddingRight="10dip">118. <TextView119. android:textAppearance="@*android:style/TextAppearance.StatusBar.Ticker" 120. android:layout_width="match_parent"121. android:layout_height="wrap_content"122. android:singleLine="true"123. />124. <TextView125. android:textAppearance="@*android:style/TextAppearance.StatusBar.Ticker" 126. android:layout_width="match_parent"127. android:layout_height="wrap_content"128. android:singleLine="true"129. />130. </com.android.systemui.statusbar.TickerView>131. </LinearLayout>132.133. <com.android.systemui.statusbar.DateView android:id="@+id/date"134. android:textAppearance="@*android:style/TextAppearance.StatusBar.Icon"135. android:layout_width="wrap_content"136. android:layout_height="match_parent"137. android:singleLine="true"138. android:gravity="center_vertical|left"139. android:paddingLeft="6px"140. android:paddingRight="6px"141. android:background="@drawable/statusbar_background"142. />143. </com.android.systemui.statusbar.StatusBarView>二为按钮添加动态效果修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarView.java 在类中新增加四个成员(须导入android.widget.ImageButton和android.content.Context):1. ImageButton mHomeBtn;2. ImageButton mBackBtn;3. ImageButton mMenuBtn;4. final Context mContext;增加三个常量:(须导入android.view.KeyEvent;)public static final int RESV_KEY_HOME = KeyEvent.KEYCODE_HOME;public static final int RESV_KEY_BACK = KeyEvent.KEYCODE_BACK;public static final int RESV_KEY_MENU = KeyEvent.KEYCODE_MENU;;在构造函数StatusBarView中初始化mContext1. public StatusBarView(Context context, AttributeSet attrs) {2. super(context, attrs);3. mContext=context;4. }注意”mContext=context;”须在”super(context, attrs);”后面,不然编译会报错在onFinishInflate中,获取几个button 的handler,并设置touch事件,添加如下代码:1. mHomeBtn = (ImageButton)findViewById(R.id.go_home);2. mBackBtn = (ImageButton)findViewById(R.id.go_back);3. mMenuBtn = (ImageButton)findViewById(R.id.pop_menu);4.5. mHomeBtn.setOnTouchListener(homeOnTouch);6. mBackBtn.setOnTouchListener(backOnTouch);7. mMenuBtn.setOnTouchListener(menuOnTouch);各button的touch事件添加如下:1. void sendIntent(Intent intent)2. {3. mContext.sendBroadcast(intent);4. }5. private void sendKeyIntent(int keycode){6. Intent intent = new Intent(Intent.ACTION_ICONKEY_CHANGED);7. intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);8. intent.putExtra("keycode", keycode);9. sendIntent(intent);10. }11.12. private OnTouchListener homeOnTouch = new OnTouchListener(){13. //@Override14. public boolean onTouch(View v, MotionEvent event)15. {16. // TODO Auto-generated method stub17. switch (event.getAction()) {18. case MotionEvent.ACTION_UP:19. {20. sendKeyIntent(RESV_KEY_HOME);21. }22. break;23. }24. return false;25. }26. };27.28. private OnTouchListener backOnTouch = new OnTouchListener(){29. //@Override30. public boolean onTouch(View v, MotionEvent event)31. {32. // TODO Auto-generated method stub33. switch (event.getAction()) {34. case MotionEvent.ACTION_UP:35. {36. sendKeyIntent(RESV_KEY_BACK);37. }38. break;39. }40. return false;41. }42. };43.44. private OnTouchListener menuOnTouch = new OnTouchListener(){45. //@Override46. public boolean onTouch(View v, MotionEvent event)47. {48. // TODO Auto-generated method stub49. switch (event.getAction()) {50. case MotionEvent.ACTION_UP:51. {52. sendKeyIntent(RESV_KEY_MENU);53. }54. break;55. }56. return false;57. }58. };为防止点击statusBar上的按钮,触发标题栏的expend事件,修改一下函数onInterceptTouchEvent,点击到不属于button区域时才允许解析Motion的event:1. public boolean onInterceptTouchEvent(MotionEvent event) {2. if( (event.getX() > mHomeBtn.getRight())3. && (event.getX() < mMenuBtn.getLeft())){4. return mService.interceptTouchEvent(event)5. ? true : super.onInterceptTouchEvent(event);6. }7. return false;8. //return mService.interceptTouchEvent(event)9. // ? true : super.onInterceptTouchEvent(event);10. }11. }需要自己添加Intent打开frameworks/base/core/java/android/content/Intent.java,增加下面的内容,由于我们的使用的API不公开,须加上/**@hide*/,不然编译会报错1. /**2. * @hide3. */4. public static final String ACTION_ICONKEY_CHANGED ="android.intent.acti on.ICONKEY_CHANGED";5.接收并处理intent修改StatusBarPolicy.java目录:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarPolicy.java首先在构造函数中加入Intent的filter,注册号这个intent的receiver。