[Pin to top] popupwindow of common android UI controls

Source: Internet
Author: User

Today we will learn how to implement the popupwindow bubble window. The effect is similar to that of the pop-up menu, but the implementation method is completely different. The following is a case of this scenario:

I. Case code

1. androidmanifest. xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android.popupwindow"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".PopupWindowActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

2. String. xml

<Resources> <string name = "app_name"> bubble window </string> <string name = "button"> open popupwindow </string> </resources>

3. Main Interface layout file: Main. xml

<?xml version="1.0" encoding="utf-8" ?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:id="@+id/main" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="openPopupWindow"        android:text="@string/button" /></LinearLayout>

4. popupwindow layout file: popupwindow. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical" Android: background = "@ drawable/background"> <! -- <Textview Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: text = "I am popupwindow"/> --> <gridview Android: id = "@ + ID/gridview" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: numcolumns = "4" Android: verticalspacing = "10dp" Android: horizontalspacing = "10dp"/> </linearlayout>

5. gridviewitem layout file: grid_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns: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:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <TextView         android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:textSize="16sp"        android:textColor="#000099"/></LinearLayout>

6. popupwindow: Enter. xml

<?xml version="1.0" encoding="utf-8"?><set xmlns: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.5"         android:toAlpha="1.0"        android:duration="300" /></set>

7. popupwindow: exit. xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false" >    <translate         android:fromYDelta="0"        android:toYDelta="100%p"        android:duration="500" />        <alpha        android:fromAlpha="1.0"         android:toAlpha="0.5"        android:duration="300" /></set>

8. popupwindowactivity. Java

Package COM. android. popupwindow; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import android. app. activity; import android. graphics. drawable. bitmapdrawable; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. g Ridview; import android. widget. listadapter; import android. widget. popupwindow; import android. widget. simpleadapter; public class popupwindowactivity extends activity {private popupwindow; private view parent; // sets the private int [] 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}; // set the gridview Item Name resource private string [] names = {"Search", "file", "Download", "full screen", "url", "bookmarks", "add ", "share"}; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // construct the popupwindow content view contentview = getlayoutinflater (). inflate (R. layout. popupwindow, null); gridview = (gridview) contentview. findviewbyid (R. id. gridview); gridview. setadapt Er (getadapter (); gridview. setonitemclicklistener (New onitemclicklistener () {@ override public void onitemclick (adapterview <?> Parent, view, int position, long ID) {If (popupwindow. isshowing () {popupwindow. dismiss () ;}}); // initialize popupwindow control popupwindow = new popupwindow (contentview, viewgroup. layoutparams. match_parent, viewgroup. layoutparams. wrap_content); // allows popupwindow to obtain the focus popupwindow. setfocusable (true); // set the popupwindow background image popupwindow. setbackgrounddrawable (New bitmapdrawable (); // set popupwindow to pop up the animation effect popupwindow. setanimationstyle (R. style. animation); // find the layout file parent = findviewbyid (R. id. main);}/*** construct the adapter used to fill the gridview data entry * @ return listadapter */private listadapter getadapter () {list 

 

Note: store, and in the drawable-hdpi folder.

Ii. Case results

Iii. Code download:Http://download.csdn.net/detail/leverage_1229/5470815

 

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.