Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1077880
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-02-26 16:17:24

软件应用中,常看到一种效果是在点击更多时,弹出一更多的弹出框,其效果截图如下:

关键代码如下:

其中MenuView是自定义的一视图,在该视图中,主要是进行图片与文字的布局,以及处理弹出popupwindow弹框

Java代码 复制代码 收藏代码
  1. intent = new Intent(this, Test2Activity.class);
  2. spec = mTabHost
  3. .newTabSpec("Test3")
  4. .setIndicator(
  5. new MenuView(this, "更多", res
  6. .getDrawable(R.drawable.pic_more)))
  7. .setContent(intent);
  8. mTabHost.addTab(spec);
  9. mTabHost.setOnTabChangedListener(this);
intent = new Intent(this, Test2Activity.class); spec = mTabHost .newTabSpec("Test3") .setIndicator( new MenuView(this, "更多", res .getDrawable(R.drawable.pic_more))) .setContent(intent); mTabHost.addTab(spec); mTabHost.setOnTabChangedListener(this);

TabChangeListener事件的处理如下:

Java代码 复制代码 收藏代码
  1. @Override
  2. public void onTabChanged(String tabId) {
  3. if(TextUtils.equals(tabId, "Test3")) {
  4. View v = mTabHost.getCurrentTabView();
  5. //设置为空的目的使得事件不再向下传播,即不会响tabchange事件
  6. v.setOnFocusChangeListener(null);
  7. }
  8. }
@Override public void onTabChanged(String tabId) { if(TextUtils.equals(tabId, "Test3")) { View v = mTabHost.getCurrentTabView(); //设置为空的目的使得事件不再向下传播,即不会响tabchange事件 v.setOnFocusChangeListener(null); } }

最后是MenuView的实现:

Java代码 复制代码 收藏代码
  1. public class MenuView extends LinearLayout implements OnItemClickListener{
  2. private PopupWindow popuwindWindow;
  3. private ArrayList> datas = null;
  4. private Context context;
  5. public MenuView(Context context,String label, Drawable icon) {
  6. super(context);
  7. this.context = context;
  8. datas = new ArrayList>();
  9. init$Views();
  10. this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
  11. LayoutParams.WRAP_CONTENT, (float) 1));
  12. setGravity(Gravity.CENTER);
  13. setOrientation(LinearLayout.VERTICAL);
  14. ImageView img = new ImageView(context);
  15. img.setImageDrawable(icon);
  16. addView(img);
  17. TextView tv = new TextView(context);
  18. tv.setText(label);
  19. tv.setGravity(Gravity.CENTER);
  20. tv.setBackgroundColor(Color.TRANSPARENT);
  21. tv.setTextColor(Color.WHITE);
  22. tv.setPadding(0,0, 0, 0);/***dimension***/
  23. tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
  24. LayoutParams.WRAP_CONTENT, (float) 0.0));
  25. addView(tv);
  26. tv.setClickable(false);
  27. tv.setFocusable(false);
  28. tv.setFocusableInTouchMode(false);
  29. img.setClickable(false);
  30. img.setFocusable(false);
  31. img.setFocusableInTouchMode(false);
  32. if(TextUtils.equals("更多", label)) {
  33. setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pub_tab_background_spec_selector));
  34. }
  35. else
  36. setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pub_tab_background_selector));
  37. }
  38. @Override
  39. public boolean onInterceptTouchEvent(MotionEvent ev) {
  40. //定义弹出时的动画
  41. popuwindWindow.setAnimationStyle(R.style.popupwindow_style);
  42. popuwindWindow.showAsDropDown(this, -popuwindWindow.getWidth(),
  43. this.getTop() + popuwindWindow.getHeight() + 5);
  44. return super.onInterceptTouchEvent(ev);
  45. }
  46. private void init$Views() {
  47. HashMap map = null;
  48. map = new HashMap();
  49. map.put("ico", R.drawable.more_help_img);
  50. map.put("tv", "帮助");
  51. datas.add(map);
  52. map = new HashMap();
  53. map.put("ico", R.drawable.more_netword_img);
  54. map.put("tv", "网络");
  55. datas.add(map);
  56. map = new HashMap();
  57. map.put("ico", R.drawable.more_update_img);
  58. map.put("tv", "更新");
  59. datas.add(map);
  60. map = new HashMap();
  61. map.put("ico", R.drawable.pic_about);
  62. map.put("tv", "关于");
  63. datas.add(map);
  64. LayoutInflater inflater = LayoutInflater.from(context);
  65. LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.test_gd, null);
  66. GridView gdv = (GridView) layout.findViewById(R.id.test_gv_id);
  67. //进行自适应屏幕的宽度
  68. int tempWidth = 60 * datas.size() + 10 + (datas.size() - 1) * 5;
  69. DisplayMetrics metrics = new DisplayMetrics();
  70. ((DashboardActivity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
  71. int disWidth = metrics.widthPixels;
  72. int width = tempWidth > disWidth ? disWidth : tempWidth;
  73. gdv.setLayoutParams(new LayoutParams(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
  74. SimpleAdapter simpleAdapter = new SimpleAdapter(context, datas,
  75. R.layout.test_gd_item, new String[] { "ico", "tv" }, new int[] {
  76. R.id.ico_id, R.id.tv_id });
  77. gdv.setAdapter(simpleAdapter);
  78. popuwindWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT,
  79. LayoutParams.WRAP_CONTENT, true);
  80. ColorDrawable color = new ColorDrawable(0);
  81. popuwindWindow.setBackgroundDrawable(color);
  82. gdv.setOnItemClickListener(this);
  83. }
  84. @Override
  85. public void onItemClick(AdapterView parent, View view, int position,
  86. long id) {
  87. popuwindWindow.dismiss();
  88. }
  89. }
public class MenuView extends LinearLayout implements OnItemClickListener{ private PopupWindow popuwindWindow; private ArrayList> datas = null; private Context context; public MenuView(Context context,String label, Drawable icon) { super(context); this.context = context; datas = new ArrayList>(); init$Views(); this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, (float) 1)); setGravity(Gravity.CENTER); setOrientation(LinearLayout.VERTICAL); ImageView img = new ImageView(context); img.setImageDrawable(icon); addView(img); TextView tv = new TextView(context); tv.setText(label); tv.setGravity(Gravity.CENTER); tv.setBackgroundColor(Color.TRANSPARENT); tv.setTextColor(Color.WHITE); tv.setPadding(0,0, 0, 0);/***dimension***/ tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, (float) 0.0)); addView(tv); tv.setClickable(false); tv.setFocusable(false); tv.setFocusableInTouchMode(false); img.setClickable(false); img.setFocusable(false); img.setFocusableInTouchMode(false); if(TextUtils.equals("更多", label)) { setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pub_tab_background_spec_selector)); } else setBackgroundDrawable(this.getResources().getDrawable(R.drawable.pub_tab_background_selector)); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { //定义弹出时的动画 popuwindWindow.setAnimationStyle(R.style.popupwindow_style); popuwindWindow.showAsDropDown(this, -popuwindWindow.getWidth(), this.getTop() + popuwindWindow.getHeight() + 5); return super.onInterceptTouchEvent(ev); } private void init$Views() { HashMap map = null; map = new HashMap(); map.put("ico", R.drawable.more_help_img); map.put("tv", "帮助"); datas.add(map); map = new HashMap(); map.put("ico", R.drawable.more_netword_img); map.put("tv", "网络"); datas.add(map); map = new HashMap(); map.put("ico", R.drawable.more_update_img); map.put("tv", "更新"); datas.add(map); map = new HashMap(); map.put("ico", R.drawable.pic_about); map.put("tv", "关于"); datas.add(map); LayoutInflater inflater = LayoutInflater.from(context); LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.test_gd, null); GridView gdv = (GridView) layout.findViewById(R.id.test_gv_id); //进行自适应屏幕的宽度 int tempWidth = 60 * datas.size() + 10 + (datas.size() - 1) * 5; DisplayMetrics metrics = new DisplayMetrics(); ((DashboardActivity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics); int disWidth = metrics.widthPixels; int width = tempWidth > disWidth ? disWidth : tempWidth; gdv.setLayoutParams(new LayoutParams(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); SimpleAdapter simpleAdapter = new SimpleAdapter(context, datas, R.layout.test_gd_item, new String[] { "ico", "tv" }, new int[] { R.id.ico_id, R.id.tv_id }); gdv.setAdapter(simpleAdapter); popuwindWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); ColorDrawable color = new ColorDrawable(0); popuwindWindow.setBackgroundDrawable(color); gdv.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { popuwindWindow.dismiss(); } }
阅读(823) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~