Pop-up window popupwindow for Android

Source: Internet
Author: User

I have been studying Android and cainiao recently, so I want to record my growth in my blog. The boss gave me a live experience: the interface design, so I have been working in the water. Now I am going to record 1.1 drops and urge myself to work hard!

I found an example on the Internet, the code is incomplete, and I checked other articles by the blogger (blog garden --- notice501), and added my own manual changes to make the changes, so I can finally run it, the running effect is as follows,

The possible cause is that the gridview settings are incorrect and the offset is not successful. However, I want to display a transparent popupwindow when I click an icon in the bottom title bar, it's not far from implementation.

Mainactivity. Java

Package me. example. popup01;

Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. gravity;
Import Android. View. keyevent;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. View. View. onclicklistener;
Import Android. View. viewgroup. layoutparams;
Import Android. widget. baseadapter;
Import Android. widget. Button;
Import Android. widget. gridview;
Import Android. widget. imageview;
Import Android. widget. popupwindow;
Import Android. widget. textview;
Public class mainactivity extends activity {
Private gridview GV;
Private button Bt1;
Private button bt2;
Private button bt3;
Private button bt4;
Private button BT5;
Private int [] icons = {R. drawable. ic_launcher, R. drawable. ic_launcher,
R. drawable. ic_launcher, R. drawable. ic_launcher,
R. drawable. ic_launcher, R. drawable. ic_launcher,
R. drawable. ic_launcher, R. drawable. ic_launcher,
R. drawable. ic_launcher };
Private string [] items = {"Browser", "image", "camera", "Clock", "Music", "market", "dialing", "information ",
"Map "};
Private popupwindow MPOP;
Private view layout;
Private void initpopwindow (){
If (MPOP = NULL ){
MPOP = new popupwindow (layout, layoutparams. wrap_content,
Layoutparams. wrap_content );
}
If (MPOP. isshowing ()){
MPOP. Dismiss ();
}
}

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );
Bt1 = (button) findviewbyid (R. Id. Bt1 );
Bt2 = (button) findviewbyid (R. Id. bt2 );
Bt3 = (button) findviewbyid (R. Id. bt3 );
Bt4 = (button) findviewbyid (R. Id. bt4 );
BT5 = (button) findviewbyid (R. Id. BT5 );
Layout = view. Inflate (this, R. layout. Window, null );
GV = (gridview) layout. findviewbyid (R. Id. gv );
Myadapter adapter = new myadapter (this, items, icons );
GV. setadapter (adapter );
Bt1.setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Initpopwindow ();
MPOP. showasdropdown (V); // use this button as anchor (which can be understood as an anchor or benchmark ).
}
});

Bt2.setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Initpopwindow ();
MPOP. showasdropdown (v, 20,-20); // horizontal axis offset 20, vertical axis-20, length of a status bar
}
});

Bt3.setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Initpopwindow ();
MPOP. showatlocation (layout,
Gravity. Center, 0, 0); // the original text is MPOP. showatlocation (popwindow. This. findviewbyid (R. Id. RL ),
// Gravity. Top | gravity. Left, 20, 20 );
An error occurred in my Eclipse, maybe because I use 2.3.3.

// Change the first parameter to layout!

}
});

Bt4.setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Initpopwindow ();
MPOP. showatlocation (layout,
Gravity. Bottom | gravity. Left, 0, 60); // at the top of the screen | right, with offset
}
});

// When the pop-up window appears, click "clearout" (BT5) to make the pop-up window disappear. The user-friendly design should be to click the blank area on the screen to disappear.
Bt5.setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
If (MPOP! = NULL ){
MPOP. Dismiss ();
}
}
});
}
Public Boolean onkeydown (INT keycode, keyevent event ){
// Intercepts button events
If (keycode = keyevent. keycode_menu ){
Initpopwindow ();
MPOP. showatlocation (layout, gravity. Bottom
| Gravity. center_horizontal, 0, 0); // at the bottom of the screen
} Else if (keycode = keyevent. keycode_back ){
If (MPOP. isshowing ()){
MPOP. Dismiss ();
} Else {
System. Exit (0 );
}
}
Return false;
}
}
// Custom Adapter
Class myadapter extends baseadapter {
Private context _ CT;
Private string [] _ items;
Private int [] _ icons;
Public myadapter (context CT, string [] items, int [] icons) // constructor. Three parameters must be input during creation.
{
_ Ct = CT;
_ Items = items;
_ Icons = icons;
}
@ Override
Public int getcount (){
Return _ items. length;
}

@ Override
Public object getitem (INT arg0 ){
Return _ items [arg0];
}
@ Override
Public long getitemid (INT position ){
Return position;
}
@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Layoutinflater factory = layoutinflater. From (_ CT );
View v = (View) Factory. Inflate (R. layout. gv, null); // bind a custom XML file. You can also create a gridview
Imageview IV = (imageview) v. findviewbyid (R. Id. Icon );
Textview TV = (textview) v. findviewbyid (R. Id. Text );
Iv. setimageresource (_ icons [position]);
TV. settext (_ items [position]);
Return V;
}
}

Activity_main.xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "horizontal"
Android: gravity = "center"
Android: Background = "@ drawable/pic0"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: Id = "@ + ID/Bt1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "btn1"
Android: textcolor = "#00 FFFF"/>
<Button
Android: Id = "@ + ID/bt2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "btn2"
Android: textcolor = "#00 FFFF"/>
<Button
Android: Id = "@ + ID/bt3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "btn3"
Android: textcolor = "#00 FFFF"/>
<Button
Android: Id = "@ + ID/bt4"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"

Android: text = "btn4"
Android: textcolor = "#00 FFFF"/>
<Button
Android: Id = "@ + ID/BT5"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "clearout"
Android: textcolor = "#00 FFFF"/>

</Linearlayout>

Window. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
>
<Gridview
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/gv"
Android: Background = "#808080"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: numcolumns = "3"
Android: gravity = "center">
</Gridview>
</Linearlayout>

GV. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Imageview
Android: Id = "@ + ID/icon"
Android: layout_width = "wrap_content"
Android: layout_height = "40px"
Android: layout_gravity = "center"
/>
<Textview
Android: Id = "@ + ID/text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: textcolor = "# ffffffff"
/>
</Linearlayout>

I just changed a small part of the original article, and I know that I am really lacking, but I will definitely do better!

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.