Android实现新浪微博中的分组菜单对话框


献上我今天刚做的一个效果Demo希望能帮助到大家。

实现效果:

 

实现思路:

分组信息其实就是一个Dialog,我们可以通过继承自Dialog来实现我们自己的需求。同时我们需要设置为当我们点击其他地方的时候Dialog能消失。

Android实现新浪微博中的分组菜单对话框源代码下载地址:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/2月/20日/Android实现新浪微博中的分组菜单对话框/

具体实现代码:(注释写在代码中)

[java]
  1. package com.jiahui.view;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Dialog;  
  9. import android.content.Context;  
  10. import android.view.Gravity;  
  11. import android.view.View;  
  12. import android.view.WindowManager.LayoutParams;  
  13. import android.widget.Button;  
  14. import android.widget.ListView;  
  15. import android.widget.SimpleAdapter;  
  16.   
  17. import com.jiahui.dialog.R;  
  18.   
  19. /** 
  20.  * 继承自Dialog 
  21.  *  
  22.  * @author Administrator 
  23.  *  
  24.  */  
  25. public class MyDialog extends Dialog {  
  26.   
  27.     protected MyDialog(Context context, boolean cancelable,  
  28.             OnCancelListener cancelListener) {  
  29.         super(context, cancelable, cancelListener);  
  30.         // TODO Auto-generated constructor stub   
  31.     }  
  32.   
  33.     public MyDialog(Context context, int theme) {  
  34.         super(context, theme);  
  35.         // TODO Auto-generated constructor stub   
  36.     }  
  37.   
  38.     public MyDialog(Context context) {  
  39.   
  40.         // 使用主题   
  41.         super(context, R.style.Theme_Transparent);  
  42.   
  43.         setContentView(R.layout.my_menu_dialog);  
  44.   
  45.         // 设置点击这个对话框之外能消失   
  46.         setCanceledOnTouchOutside(true);  
  47.         // 设置window属性   
  48.         LayoutParams a = getWindow().getAttributes();  
  49.         a.gravity = Gravity.TOP;  
  50.         a.dimAmount = 0// 去背景遮盖   
  51.   
  52.         getWindow().setAttributes(a);  
  53.   
  54.         initMenu();  
  55.   
  56.     }  
  57.   
  58.     private void initMenu() {  
  59.   
  60.         List<String> menus = new ArrayList<String>();  
  61.   
  62.         menus.add("分组一");  
  63.         menus.add("分组二");  
  64.   
  65.         // 准备要添加的数据条目   
  66.         List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();  
  67.   
  68.         for (String str : menus) {  
  69.             Map<String, Object> map = new HashMap<String, Object>();  
  70.   
  71.             map.put("group", str);  
  72.             items.add(map);  
  73.         }  
  74.         SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), items,  
  75.                 R.layout.menu_item, new String[] { "group" },  
  76.                 new int[] { R.id.item_text });  
  77.   
  78.         ListView mylistview = (ListView) this.findViewById(R.id.mylistview);  
  79.   
  80.         mylistview.setAdapter(simpleAdapter);  
  81.   
  82.     }  
  83.   
  84.     // 设置位置   
  85.     public void setPosition(int x, int y) {  
  86.         LayoutParams a = getWindow().getAttributes();  
  87.         if (-1 != x)  
  88.             a.x = x;  
  89.         if (-1 != y)  
  90.             a.y = y;  
  91.         System.out.println("a.x" + a.x);  
  92.         System.out.println("a.y" + a.y);  
  93.         getWindow().setAttributes(a);  
  94.     }  
  95.   
  96. }  

My_menu_dialog.xml文件中的代码:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.     android:id="@+id/layout_root"  
  6.   
  7.     android:layout_width="wrap_content"  
  8.   
  9.     android:layout_height="wrap_content" >  
  10.   
  11.    
  12.   
  13.     <ListView  
  14.   
  15.         android:id="@+id/mylistview"  
  16.   
  17.         android:layout_width="wrap_content"  
  18.   
  19.         android:layout_height="wrap_content"  
  20.   
  21.         android:gravity="center" >  
  22.   
  23.     </ListView>  
  24.   
  25.    
  26.   
  27.     <Button  
  28.   
  29.         android:id="@+id/close_menu"  
  30.   
  31.         android:layout_width="wrap_content"  
  32.   
  33.         android:layout_height="wrap_content"  
  34.   
  35.         android:text="close"  
  36.   
  37.         android:visibility="gone" />  
  38.   
  39.    
  40.   
  41. </RelativeLayout>  

Theme.xml文件

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <resources>  
  4.   
  5.     <style name="Theme.Transparent" parent="android:Theme">  
  6.   
  7.         <item name="android:windowBackground">@drawable/dialog_box_2</item>  
  8.   
  9.         <item name="android:windowIsTranslucent">false</item>  
  10.   
  11.         <item name="android:windowContentOverlay">@null</item>  
  12.   
  13.         <item name="android:windowNoTitle">true</item>  
  14.   
  15.         <item name="android:windowIsFloating">true</item>  
  16.   
  17.         <item name="android:backgroundDimEnabled">false</item>  
  18.   
  19.     </style>  
  20.   
  21. </resources>  

测试的Activity代码

[java]
  1. package com.jiahui.dialog;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Rect;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Window;  
  9. import android.widget.Button;  
  10.   
  11. import com.jiahui.view.MyDialog;  
  12.   
  13. public class MyDialogActivity extends Activity {  
  14.   
  15.     private MyDialog myDialog;  
  16.   
  17.     private Button btngroup;  
  18.   
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         //设置无标题    
  22.         requestWindowFeature(getWindow().FEATURE_NO_TITLE);  
  23.         setContentView(R.layout.main);  
  24.   
  25.         btngroup = (Button) this.findViewById(R.id.btngroup);  
  26.   
  27.         btngroup.setOnClickListener(new OnClickListener() {  
  28.   
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 if (null == myDialog) {  
  32.                     myDialog = new MyDialog(MyDialogActivity.this);  
  33.   
  34.       
  35.                     //如果没有设置无标题的话,这里还要加上标题栏的高度才行,至于如何获取标题栏的高度的话由读者自行解决   
  36.                     int top = btngroup.getTop();//是获取如果设置了的margin_top   
  37.                     int height = btngroup.getHeight();  
  38.                     int y= top + height;  
  39.                       
  40.                     System.out.println("y:"+y);  
  41.                     // 设置显示的位置   
  42.                     myDialog.setPosition(-1, y);  
  43.   
  44.                 }  
  45.                 if (myDialog.isShowing()) {  
  46.                     myDialog.dismiss();  
  47.                 } else {  
  48.                     myDialog.show();  
  49.                 }  
  50.   
  51.             }  
  52.         });  
  53.     }  
  54. }  

相关内容