Android Learning Note---63-popupwindow, bubble window implementation

Source: Internet
Author: User

Reprint http://blog.csdn.net/lidew521/article/details/8976627

Popupwindow is a floating container that can be displayed on top of the current activity, the position of the Popupwindow popup can be changed, according to whether there is no offset, can be divided into non-offset and a cheap two kinds , depending on the reference object, can be divided into two types: relative to the position of a control (anchor anchor Point) and the relative position within the parent container.

Create Android App: Project name:popupwindow, android2.2,application Name: Bubble window, Packagename:cn.itcast.popwindow,createactivity:mainactivity.

1. Interface

Add a button to the interface and click to display the Pop window.

/popupwindow/res/values/strings.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<string name= "Hello" >hello world,mainactivity!</string>

<string name= "app_name" > Bubble window </string>

<string name= "button" > Open Bubble Window </string>

</resources>

/popupwindow/res/layout/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= "Fill_parent"

android:orientation= "Vertical"

Android:id= "@+id/main"

>

<button

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "@string/button"

android:onclick= "Openpopwindow"

/>

</LinearLayout>

2. Implement button click Method

Create Popupwindow:

Layoutinflater Mlayoutinflater = (layoutinflater) context.getsystemservice (Layout_inflater_service);

View Contentview = mlayoutinflater.inflate (r.layout.xxx,null); R.layout.xxx for interface files

Popupwindow = Newpopupwindow (Contentview, viewgroup.layoutparams.match_parent,viewgroup.layoutparams.wrap_content );

Popupwindow.setbackgrounddrawable (Newbitmapdrawable ());

Popupwindow.setfocusable (TRUE);//Get focus, create Popupwindow default no focus

Ways to display Popupwindow:

Showasdropdown (Viewanchor) relative to a control's position (directly below), no offset

Showasdropdown (viewanchor, int xoff, int yoff) relative to a control's position, offset, Xoff x-axis offset, yoff y-axis offset

Showatlocation (viewparent, int gravity, int x, int y) where in the parent container, gravity is the relative position, such as: Central Gravity.center, Lower Gravity.bottom, Gravity.right| Gravity.bottom the bottom right and so on, the back two parameters are the offset of the X/Y axis.

Close Popupwindow:dismiss ()

/popupwindow/src/cn/itcast/popwindow/mainactivity.java

Package Cn.itcast.popwindow;

Import java.util.ArrayList;

Import Java.util.HashMap;

Import java.util.List;

Import android.app.Activity;

importandroid.graphics.drawable.BitmapDrawable;

Import Android.os.Bundle;

Import android.view.Gravity;

Import Android.view.View;

Import Android.view.ViewGroup;

Import Android.widget.AdapterView;

Importandroid.widget.AdapterView.OnItemClickListener;

Import Android.widget.GridView;

Import Android.widget.ListAdapter;

Import Android.widget.PopupWindow;

Import Android.widget.SimpleAdapter;

public class Mainactivity Extendsactivity {

Popupwindowpopupwindow;

Viewparent;

privateint[] Images ={r.drawable.i1,r.drawable.i2,r.drawable.i3,r.drawable.i4,r.drawable.i5,r.drawable.i6, R.DRAWABLE.I7,R.DRAWABLE.I8};

Privatestring[] names = {"Search", "File Management", "Download Management", "full Screen", "url", "Bookmark", "Add Bookmark", "Share Page"};

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

Viewcontentview = Getlayoutinflater (). Inflate (R.layout.popwindow, NULL);

GridView GridView = (GridView) Contentview.findviewbyid (R.id.gridview);

Gridview.setadapter (Getadapter ());

Gridview.setonitemclicklistener (New Itemclicklistener ());

Popupwindow = new Popupwindow (contentview,viewgroup.layoutparams.match_parent, ViewGroup.LayoutParams.WRAP_CONTENT );

Popupwindow.setfocusable (TRUE);//Get focus

Popupwindow.setbackgrounddrawable (Newbitmapdrawable ()); Click the blank place to close Popupwindow

Popupwindow.setanimationstyle (r.style.animation);

Parent =this.findviewbyid (R.id.main);

}

Private Final class Itemclicklistener implements onitemclicklistener{

Publicvoid Onitemclick (adapterview<?> parent, view view, int position, long ID) {

if (popupwindow.isshowing ()) Popupwindow.dismiss ();//Close

//....

}

}

Private ListAdapter Getadapter() {

list

for (int i = 0; I <images.length; i++) {

hashmap<string,object> item = new hashmap<string, object> ();

Item.put ("image", images[i]);

Item.put ("name", Names[i]);

Data.add (item);

}

Simpleadaptersimpleadapter = new Simpleadapter (this, data, R.layout.grid_item,

newstring[]{"image", "Name"}, new Int[]{r.id.imageview,r.id.textview});

Returnsimpleadapter;

}

Publicvoid Openpopwindow(VIEWV) {

Popupwindow.showatlocation (parent,gravity.bottom, 0, 0);

}

}

3. Pop-up windows

/popupwindow/res/layout/popwindow.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

android:orientation= "Vertical"

android:background= "@drawable/bg"

>

<gridview

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

Android:numcolumns= "4"

Android:horizontalspacing= "10DP"

Android:verticalspacing= "10DP"

Android:id= "@+id/gridview"

/>

</LinearLayout>

/popupwindow/res/drawable/bg.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<shape

Xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:shape= "Rectangle" >

<gradient

Android:angle= "270"

Android:endcolor= "#1DC9CD"

Android:startcolor= "#A2E0FB"/>

<padding

android:left= "2DP"

android:top= "2DP"

android:right= "2DP"

android:bottom= "2DP"/>

</shape>

Gridview:

/popupwindow/res/layout/grid_item.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

android:orientation= "Vertical"

android:gravity= "Center"

>

<imageview

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:id= "@+id/imageview"

/>

<textview

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:gravity= "Center"

Android:textsize= "16SP"

Android:textcolor= "#000099"

Android:id= "@+id/textview"

/>

</LinearLayout>

4. Images to use

/popupwindow/res/drawable/i1.png

/popupwindow/res/drawable/i2.png

/popupwindow/res/drawable/i3.png

/popupwindow/res/drawable/i4.png

/popupwindow/res/drawable/i5.png

/popupwindow/res/drawable/i6.png

/popupwindow/res/drawable/i7.png

/popupwindow/res/drawable/i8.png

5. Show/Hide Animations

/popupwindow/res/values/styles.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<style name= "animation" >

<itemname= "Android:windowenteranimation" >@anim/enter</item>

<itemname= "Android:windowexitanimation" >@anim/out </item>

</style>

</resources>

/popupwindow/res/anim/enter.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<setxmlns:android= "Http://schemas.android.com/apk/res/android"

Android:shareinterpolator= "false" >

<translate

Android:fromydelta= "100%p"

Android:toydelta= "0"

android:duration= "500"

/>

<alpha

Android:fromalpha= "0.7"

Android:toalpha= "1.0"

android:duration= "300"

/>

</set>

/popupwindow/res/anim/out.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<setxmlns:android= "Http://schemas.android.com/apk/res/android"

Android:shareinterpolator= "false" >

<translate

Android:fromydelta= "0"

Android:toydelta= "100%p"

Android:duration= "3000"

/>

<alpha

Android:fromalpha= "1.0"

Android:toalpha= "0.5"

Android:duration= "2000"

/>

</set>

The location map is as follows:

Display effect:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Learning Note---63-popupwindow, bubble window implementation

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.