Android实现新浪微博中的分组菜单对话框
fmms
13年前
实现效果:
实现思路:
分组信息其实就是一个Dialog,我们可以通过继承自Dialog来实现我们自己的需求。同时我们需要设置为当我们点击其他地方的时候Dialog能消失。
具体实现代码:(注释写在代码中)
package com.jiahui.view; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Dialog; import android.content.Context; import android.view.Gravity; import android.view.View; import android.view.WindowManager.LayoutParams; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import com.jiahui.dialog.R; /** * 继承自Dialog * * @author Administrator * */ public class MyDialog extends Dialog { protected MyDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); // TODO Auto-generated constructor stub } public MyDialog(Context context, int theme) { super(context, theme); // TODO Auto-generated constructor stub } public MyDialog(Context context) { // 使用主题 super(context, R.style.Theme_Transparent); setContentView(R.layout.my_menu_dialog); // 设置点击这个对话框之外能消失 setCanceledOnTouchOutside(true); // 设置window属性 LayoutParams a = getWindow().getAttributes(); a.gravity = Gravity.TOP; a.dimAmount = 0; // 去背景遮盖 getWindow().setAttributes(a); initMenu(); } private void initMenu() { List<String> menus = new ArrayList<String>(); menus.add("分组一"); menus.add("分组二"); // 准备要添加的数据条目 List<Map<String, Object>> items = new ArrayList<Map<String, Object>>(); for (String str : menus) { Map<String, Object> map = new HashMap<String, Object>(); map.put("group", str); items.add(map); } SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), items, R.layout.menu_item, new String[] { "group" }, new int[] { R.id.item_text }); ListView mylistview = (ListView) this.findViewById(R.id.mylistview); mylistview.setAdapter(simpleAdapter); } // 设置位置 public void setPosition(int x, int y) { LayoutParams a = getWindow().getAttributes(); if (-1 != x) a.x = x; if (-1 != y) a.y = y; System.out.println("a.x" + a.x); System.out.println("a.y" + a.y); getWindow().setAttributes(a); } }My_menu_dialog.xml文件中的代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ListView android:id="@+id/mylistview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" > </ListView> <Button android:id="@+id/close_menu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="close" android:visibility="gone" /> </RelativeLayout>Theme.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.Transparent" parent="android:Theme"> <item name="android:windowBackground">@drawable/dialog_box_2</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">false</item> </style> </resources>测试的Activity代码
package com.jiahui.dialog; import android.app.Activity; import android.graphics.Rect; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import com.jiahui.view.MyDialog; public class MyDialogActivity extends Activity { private MyDialog myDialog; private Button btngroup; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置无标题 requestWindowFeature(getWindow().FEATURE_NO_TITLE); setContentView(R.layout.main); btngroup = (Button) this.findViewById(R.id.btngroup); btngroup.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (null == myDialog) { myDialog = new MyDialog(MyDialogActivity.this); //如果没有设置无标题的话,这里还要加上标题栏的高度才行,至于如何获取标题栏的高度的话由读者自行解决 int top = btngroup.getTop();//是获取如果设置了的margin_top int height = btngroup.getHeight(); int y= top + height; System.out.println("y:"+y); // 设置显示的位置 myDialog.setPosition(-1, y); } if (myDialog.isShowing()) { myDialog.dismiss(); } else { myDialog.show(); } } }); } }
提供源代码下载:
http://download.csdn.net/detail/jiahui524/4071659
如需转载引用请注明出处:http://blog.csdn.net/jiahui524