I haven't updated my blog for a long time. Now I want to present an effect demo that I just created today to help you.
Effect:
Implementation ideas:
The group information is actually a dialog. We can inherit from the dialog to achieve our own needs. At the same time, we need to set the dialog to disappear when we click somewhere else.
Specific implementation code: (comment in the Code)
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 ;/* ** Inherited from dialog ** @ author administrator **/public class mydialog extends dialog {protected mydialog (context, Boolean cancelable, oncancellistener cancellistener) {super (context, cancelable, cancellistener); // todo auto-generated constructor stub} public mydialog (context, int theme) {super (context, theme ); // todo auto-generated constructor stub} public mydialog (context) {// use Topic super (context, R. style. theme_transparent); setcontentview (R. layout. my_menu_dialog); // set setcanceledontouchoutside (true) to disappear after clicking this dialog box; // set the window attribute layoutparams A = getwindow (). getattributes ();. gravity = gravity. top;. dimamount = 0; // mask getwindow () with the background (). setattributes (a); initmenu ();} private void initmenu () {list <string> menus = new arraylist <string> (); menus. add ("group 1"); menus. add ("group 2"); // prepare the data entry to be added Li St <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 = 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);} // set the position public void setposition (int x, int y) {layoutparams A = getwindow (). getattributes (); If (-1! = X) A. X = x; If (-1! = Y). y = y; system. out. println (". X "+. x); system. out. println (". Y "+. y); getwindow (). setattributes ();}}
Code in the my_menu_dialog.xml file:
<?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 file
<?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>
Tested activity code
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; private button btngroup; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // set requestwindowfeature (getwindow () without the title (). feature_no_title); setcontentview (R. layout. main); btngroup = (button) This. findviewbyid (R. id. btngroup); btngroup. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {If (null = mydialog) {mydialog = new mydialog (mydialogactivity. this); // if no title is set, the height of the title bar must be added here. For how to obtain the height of the title bar, the reader should solve int Top = btngroup by himself. gettop (); // get if the margin_topint Height = btngroup is set. getheight (); int y = Top + height; system. out. println ("Y:" + Y); // you can specify the position of mydialog. setposition (-1, Y);} If (mydialog. isshowing () {mydialog. dismiss ();} else {mydialog. show ();}}});}}
Continue with my blog style. Sharing is the foundation of happiness. There are pictures and truth!
Download source code:
Http://download.csdn.net/detail/jiahui524/4071659
If you need to reprint reference please indicate the source: http://blog.csdn.net/jiahui524
You are welcome to have more exchanges. Let cainiao grow together!
PS: Another method is to use popupwindow.
Http://blog.csdn.net/jiahui524/article/details/7298690