Android Simple Package A beautiful, easy-to-use menu-type Popupwindow

Source: Internet
Author: User

First on:

Is such a menu-type of Popupwindow, now how to do it,
First look at a layout file, this layout file

 PackageCn.edu.jxufe.popupwindowdemo;ImportAndroid.content.Context;Importandroid.graphics.drawable.BitmapDrawable;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.PopupWindow;ImportAndroid.widget.TextView;ImportJava.util.ArrayList;ImportJava.util.List; Public  class popupwindowutil {    PrivateListView ListView;PrivatePopupwindow window;//Window offset at x-axis    Private intXOFF =0;//The offset of the window on the y-axis    Private intYoff =0; Public Popupwindowutil(context context, list<string> datas) {window =NewPopupwindow (context);//viewgroup.layoutparams.wrap_content, automatic wrapping of all contentWindow.setheight (ViewGroup.LayoutParams.WRAP_CONTENT); Window.setfocusable (true);//When you click the Back button, the window will disappear automatically.Window.setbackgrounddrawable (NewBitmapdrawable ()); View Localview = layoutinflater.from (context). Inflate (R.layout.lv_pw_menu,NULL);        ListView = (ListView) Localview.findviewbyid (R.id.lv_pop_list); Listview.setadapter (NewMyadapter (context, datas)); Listview.settag (window);//Set the displayed viewWindow.setcontentview (Localview); } Public void Setitemclicklistener(Adapterview.onitemclicklistener Listener)    {Listview.setonitemclicklistener (listener); } Public void Dismiss() {Window.dismiss (); }/** * @param xOff X-axis (left/right) offset * @param yoff y-axis (up/down) offset */     Public void Setoff(intXOFF,intYoff) { This. xOff = XOFF; This. Yoff = Yoff; }/** * @param Paramview Click button */     Public void Show(View Paramview,intCount) {//The count is to manually adjust the width of the windowWindow.setwidth (Paramview.getwidth () * count);//Set window display position, back two 0 is the offset, can be set freelyWindow.showasdropdown (Paramview, XOFF, Yoff);//Update window statusWindow.update (); } class Myadapter extends Baseadapter {PrivateContext context;PrivateList<string> Mdatas; Public Myadapter(context context, list<string> datas) { This. Context = Context;if(Datas = =NULL) {datas =NewArraylist<> ();        } Mdatas = Datas; }@Override         Public int GetCount() {returnMdatas.size (); }@Override         PublicObjectGetItem(intPosition) {returnMdatas.get (position); }@Override         Public Long Getitemid(intPosition) {returnPosition }@Override         PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {TextView tvitem;if(Convertview = =NULL{Convertview = Layoutinflater.from (context). Inflate (R.layout.lv_item_pw_menu,NULL);                Tvitem = (TextView) Convertview.findviewbyid (R.id.tv_item_pw_menu);            Convertview.settag (Tvitem); }Else{Tvitem = (TextView) convertview.gettag (); } tvitem.settext (GetItem (position) +"");returnConvertview; }    }}

and see how it's called in activity.

 Public  class menuactivity extends Activity {    PrivateContext context = This;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);    Setcontentview (R.layout.activity_menu); }//Menu button onclick event     Public void Menuclick(View view) {Finallist<string> items =NewArraylist<> (); Items.Add ("First item"); Items.Add ("Second Item"); Items.Add ("Third item"); Items.Add ("Item Fourth");FinalPopupwindowutil Popupwindow =NewPopupwindowutil (context, items); Popupwindow.setitemclicklistener (NewAdapterview.onitemclicklistener () {@Override             Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {Popupwindow.dismiss (); Toast.maketext (Context,"you clicked."+ items.get (position), Toast.length_short). Show (); }        });//Adjust the width of the window manually according to the following numbersPopupwindow.show (View,4); }}

Next is the layout file Lv_item_pw_menu
The item layout of the ListView

<?xml version= "1.0" encoding= "Utf-8"?><relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:gravity  =" center_vertical " android:layout_width  = "match_parent"  android:layout_marginleft  = "8DP"  android:layout_marginright  =" 8DP " android:background  = "@android: Color/transparent"  android:layout_marginbottom  = "8DP"  Span class= "Hljs-attribute" >android:layout_height  = "wrap_content"  >     <textview  android:layout _width  = "match_parent"  android:gravity  = "center"  android:drawablepadding  =< Span class= "Hljs-value" > "3DP"  android:padding  =         "7DP"  android:text  = "test data"  android:id  =" @+id/tv_item_pw_menu " android:textsize  =" 18sp "  Android:layout_height  = "wrap_content" /> </relativelayout>

The layout of the window Lv_pw_menu.xml

<?xml version= "1.0" encoding= "Utf-8"?><LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"    android:orientation="Vertical"    Android:layout_width="Match_parent"    Android:background="@drawable/menu"    Android:layout_height="Match_parent"    Android:paddingbottom="15.0dip"    Android:paddingleft="6.0dip"    Android:paddingright="6.0dip"    Android:paddingtop="20.0dip">    <ListViewandroid:id= "@+id/lv_pop_list"android:layout_width=" Match_parent "android:layout_height=" Match_parent "android:cachecolorhint=" @ Null "android:scrollbars=" None " />                                        </linearlayout>

In this way, you can call it directly in the activity and do not need to modify it in the tool class.

Attached links, only code and pictures:
http://download.csdn.net/detail/chang_1134/9516120

Android Simple Package A beautiful, easy-to-use menu-type Popupwindow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.