Android component Popupwindow use method detailed _android

Source: Internet
Author: User
Tags static class

First look at the effect:

Now a lot of applications need to do more dazzle, such as UC, and listen every day, the effect is very cool, the source code has been opened to the outside, interested can go to study the

On the source

Main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" fill_parent "
 android:layout_height=" 500DP "
 android:orientation= "Vertical" 
 android:background= "@android: Color/white"
 >

 <include android:id= "@+id/top_title"
 
 layout= "@layout/title_two_button"/>
 <button 
 android:id= "@+id/bb"
 android:layout_width = "Fill_parent"
 android:layout_height= "wrap_content"
 android:text= "button"
 />
</ Linearlayout>

Title_two_button.xml

<?xml version= "1.0" encoding= "Utf-8"?> <framelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "> <textview android:id=" @+id /tvtitle "android:layout_width=" fill_parent "android:layout_height=" 45dip "android:background=" @drawable/search_ BG "android:gravity=" center "android:text=" android:textcolor= "@color/black" android:textsize= "25dip"/> ; Button android:id= "@+id/btn_title_left" android:layout_width= "49dip" android:layout_height= "36dip" Android:layout_ Gravity= "center_vertical" android:background= "@drawable/title_button" android:textcolor= "@color/white"/> < Button android:id= "@+id/btn_title_right" android:layout_width= "49dip" android:layout_height= "36dip" android:layout _gravity= "Right" android:layout_marginright= "2dip" android:layout_margintop= "4dip" android:background= "@drawable Title_button2 "android:textcolor=" @color/white "/> </Framelayout>
 

Group_list.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:orientation= "vertical" 0.0sp "android:paddingright=" 0.0sp "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android : layout_margin= "0.0px" xmlns:android= "http://schemas.android.com/apk/res/android" android:background= "@drawable GROUP_BG "> <imageview android:id=" @+id/iv_group_list_bg_divider "android:background=" @drawable/group_ Divider "android:padding=" 0.0px "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:l" ayout_margin= "0.0px"/> <listview android:id= "@+id/lvgroup" android:fadingedgelength= "0.0SP" Android:layout_w Idth= "Fill_parent" android:layout_height= "fill_parent" android:listselector= "@drawable/grouplist_item_bg" Android :d rawselectorontop= "true" android:cachecolorhint= "#00000000" android:divider= "@drawable/group_divider" Android: dividerheight= "2.0px" android:layout_weight= "0.0"/> </lInearlayout>
 

Group_item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:orientation=
"vertical" android:layout_ Width= "Fill_parent" android:layout_height= "@dimen/group_item_height"
 xmlns:android= "http:// Schemas.android.com/apk/res/android ">
 <textview 
 android:gravity=" center " 
 android:id=" @+id/ Groupitem " 
 android:layout_width=" fill_parent " 
 android:layout_height=" fill_parent " 
 style=" @style Grouplist_item_textview "/>
</LinearLayout>

All right, these are the layout files that are used here, start the plays

Package jk.activity;
Import java.util.ArrayList;

Import java.util.List;
Import android.app.Activity;
Import Android.content.Context;
Import android.graphics.drawable.BitmapDrawable;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.WindowManager;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.Button;
Import Android.widget.ListView;
Import Android.widget.PopupWindow;
Import Android.widget.TextView;

Import Android.widget.Toast;

 public class Poupwindowdemoactivity extends activity {private Popupwindow Popupwindow;

 Private ListView Lv_group;

 Private view view;

 Private View Top_title;

 Private TextView Tvtitle;
 
 Private list<string> groups;
 Private button button;
 public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main); ButtOn = (Button) Findviewbyid (R.ID.BB);

 Top_title = This.findviewbyid (R.id.top_title);

 Tvtitle = (TextView) Top_title.findviewbyid (r.id.tvtitle);

 Tvtitle.settext ("the most");
 Tvtitle.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {ShowWindow (v);

 }
 }); /** * Show * @param parent/private void ShowWindow (View parent) {if (Popupwindow = null) {Layoutinflate

 R layoutinflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);

 View = Layoutinflater.inflate (r.layout.group_list, NULL);
 Lv_group = (ListView) View.findviewbyid (R.id.lvgroup);
 Load Data groups = new arraylist<string> ();
 Groups.add ("daughter Country");
 Groups.add ("West Tour");
 Groups.add ("Dahua");

 Groups.add ("Datang");
 Groupadapter groupadapter = new Groupadapter (this, groups);
 Lv_group.setadapter (Groupadapter);
 Create a Popuwidow object Popupwindow = new Popupwindow (view, 200, 250);
 //Make it clustered popupwindow.setfocusable (true); Set allow out of click to disappear Popupwindow.setoutsiDetouchable (TRUE);
 This is to click "Back" to make it disappear, and it will not affect your background popupwindow.setbackgrounddrawable (new bitmapdrawable ());
 WindowManager WindowManager = (windowmanager) getsystemservice (Context.window_service); The position shown is: Half of the width of the screen-popupwindow half of the height of the int xpos = Windowmanager.getdefaultdisplay (). GetWidth ()/2-popupwindow.getwidth

 ()/2; LOG.I ("Coder", "Windowmanager.getdefaultdisplay (). GetWidth ()/2:" + Windowmanager.getdefaultdisplay (). GetWidth ()/2
 );

 LOG.I ("Coder", "popupwindow.getwidth ()/2:" + popupwindow.getwidth ()/2);

 LOG.I ("Coder", "xpos:" + xpos);

 Popupwindow.showasdropdown (parent, xpos, 0); Lv_group.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> Adapterview, view view, int position, long id) {Toast.maketext (poupwindowdemoactivity.this, groups.get (position) +

 Groups.get (position), 1000). Show ();
 if (Popupwindow!= null) {Popupwindow.dismiss ();
 }
 }
 });

 }
}

Here also need to use the adapter, use more generally have baseadapter,simpleadapter,baseadapter quite powerful, but simpleadapter extensibility is very good, can meet the custom demand, If you understand that the input-output stream will soon understand adapter, he is the

of the equipment data.

Package jk.activity;



Import java.util.List;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;

Import Android.widget.TextView;

 public class Groupadapter extends Baseadapter {private context context;

 Private list<string> List;
 Public Groupadapter (context context, list<string> List) {this.context = context;

 This.list = list;
 @Override public int GetCount () {return list.size ();
 @Override public Object getitem (int position) {return list.get (position);
 @Override public long getitemid (int position) {return position;
 @Override public View getview (int position, View Convertview, ViewGroup viewgroup) {Viewholder holder;
 if (convertview==null) {convertview=layoutinflater.from (context). Inflate (R.layout.group_item_view, NULL);
 
 Holder=new Viewholder ();
 
 Convertview.settag (holder); Holder.groupitem= (TextView) CONVERTVIEw.findviewbyid (R.id.groupitem);
 } else{holder= (Viewholder) Convertview.gettag ();
 
 } holder.groupItem.setText (List.get (position));
 return convertview;
 Static class Viewholder {TextView groupitem;
 }

}

For adapter, the most important thing is getview (int position, View Convertview, ViewGroup viewgroup) This method, the dish has been finished, you enjoy slowly.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.