歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現新浪微博中的分組菜單對話框

Android實現新浪微博中的分組菜單對話框

日期:2017/3/1 10:30:25   编辑:Linux編程
獻上我今天剛做的一個效果Demo希望能幫助到大家。

實現效果:

實現思路:

分組信息其實就是一個Dialog,我們可以通過繼承自Dialog來實現我們自己的需求。同時我們需要設置為當我們點擊其他地方的時候Dialog能消失。

Android實現新浪微博中的分組菜單對話框源代碼下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/2月/20日/Android實現新浪微博中的分組菜單對話框/

具體實現代碼:(注釋寫在代碼中)

[java]
  1. package com.jiahui.view;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.app.Dialog;
  7. import android.content.Context;
  8. import android.view.Gravity;
  9. import android.view.View;
  10. import android.view.WindowManager.LayoutParams;
  11. import android.widget.Button;
  12. import android.widget.ListView;
  13. import android.widget.SimpleAdapter;
  14. import com.jiahui.dialog.R;
  15. /**
  16. * 繼承自Dialog
  17. *
  18. * @author Administrator
  19. *
  20. */
  21. public class MyDialog extends Dialog {
  22. protected MyDialog(Context context, boolean cancelable,
  23. OnCancelListener cancelListener) {
  24. super(context, cancelable, cancelListener);
  25. // TODO Auto-generated constructor stub
  26. }
  27. public MyDialog(Context context, int theme) {
  28. super(context, theme);
  29. // TODO Auto-generated constructor stub
  30. }
  31. public MyDialog(Context context) {
  32. // 使用主題
  33. super(context, R.style.Theme_Transparent);
  34. setContentView(R.layout.my_menu_dialog);
  35. // 設置點擊這個對話框之外能消失
  36. setCanceledOnTouchOutside(true);
  37. // 設置window屬性
  38. LayoutParams a = getWindow().getAttributes();
  39. a.gravity = Gravity.TOP;
  40. a.dimAmount = 0; // 去背景遮蓋
  41. getWindow().setAttributes(a);
  42. initMenu();
  43. }
  44. private void initMenu() {
  45. List<String> menus = new ArrayList<String>();
  46. menus.add("分組一");
  47. menus.add("分組二");
  48. // 准備要添加的數據條目
  49. List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
  50. for (String str : menus) {
  51. Map<String, Object> map = new HashMap<String, Object>();
  52. map.put("group", str);
  53. items.add(map);
  54. }
  55. SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), items,
  56. R.layout.menu_item, new String[] { "group" },
  57. new int[] { R.id.item_text });
  58. ListView mylistview = (ListView) this.findViewById(R.id.mylistview);
  59. mylistview.setAdapter(simpleAdapter);
  60. }
  61. // 設置位置
  62. public void setPosition(int x, int y) {
  63. LayoutParams a = getWindow().getAttributes();
  64. if (-1 != x)
  65. a.x = x;
  66. if (-1 != y)
  67. a.y = y;
  68. System.out.println("a.x" + a.x);
  69. System.out.println("a.y" + a.y);
  70. getWindow().setAttributes(a);
  71. }
  72. }

My_menu_dialog.xml文件中的代碼:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/layout_root"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content" >
  6. <ListView
  7. android:id="@+id/mylistview"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:gravity="center" >
  11. </ListView>
  12. <Button
  13. android:id="@+id/close_menu"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="close"
  17. android:visibility="gone" />
  18. </RelativeLayout>

Theme.xml文件

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

測試的Activity代碼

[java]
  1. package com.jiahui.dialog;
  2. import android.app.Activity;
  3. import android.graphics.Rect;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.view.Window;
  8. import android.widget.Button;
  9. import com.jiahui.view.MyDialog;
  10. public class MyDialogActivity extends Activity {
  11. private MyDialog myDialog;
  12. private Button btngroup;
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. //設置無標題
  16. requestWindowFeature(getWindow().FEATURE_NO_TITLE);
  17. setContentView(R.layout.main);
  18. btngroup = (Button) this.findViewById(R.id.btngroup);
  19. btngroup.setOnClickListener(new OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. if (null == myDialog) {
  23. myDialog = new MyDialog(MyDialogActivity.this);
  24. //如果沒有設置無標題的話,這裡還要加上標題欄的高度才行,至於如何獲取標題欄的高度的話由讀者自行解決
  25. int top = btngroup.getTop();//是獲取如果設置了的margin_top
  26. int height = btngroup.getHeight();
  27. int y= top + height;
  28. System.out.println("y:"+y);
  29. // 設置顯示的位置
  30. myDialog.setPosition(-1, y);
  31. }
  32. if (myDialog.isShowing()) {
  33. myDialog.dismiss();
  34. } else {
  35. myDialog.show();
  36. }
  37. }
  38. });
  39. }
  40. }
Copyright © Linux教程網 All Rights Reserved