Android Popwindow implementation of the left pop-up menu layer and Popupwindow Main method Introduction _android

Source: Internet
Author: User
Tags stub xmlns

Popupwindow can achieve a floating layer effect, the main methods are: Can customize view, through the Layoutinflator method, can appear and exit animation, you can specify the display location.

In order to Popupwindow multiple functions and strive to use simple code to achieve, wrote a click on the left side of the button pop-up menu function, to achieve the appearance and exit when the animation effect and click on other areas when the pop-up layer automatically disappear, the effect chart is as follows:
Source:
1.popwindowonleftactivity.java

Copy Code code as follows:

Package com.pop.main;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.View.OnTouchListener;
Import Android.widget.Button;
Import Android.widget.PopupWindow;
public class Popwindowonleftactivity extends activity {
Declaring a reference to a Popupwindow object
Private Popupwindow Popupwindow;
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Click on the button pop-up menu
Button pop = (Button) Findviewbyid (R.ID.POPBTN);
Pop.setonclicklistener (Popclick);
}
Click the pop-up left menu to display the way
Onclicklistener Popclick = new Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Getpopupwindow ();
Here is the position display way, in the lower left corner of the button
Popupwindow.showasdropdown (v);
Here you can try other effects, such as Popupwindow.showasdropdown (V,
(screenwidth-dialgowidth)/2, 0);
Popupwindow.showatlocation (Findviewbyid (r.id.layout),
Gravity.center, 0, 0);
}
};
/**
* Create Popupwindow
*/
protected void Initpopuptwindow () {
TODO auto-generated Method Stub
Get a view of the custom layout file Pop.xml
View Popupwindow_view = Getlayoutinflater (). Inflate (R.layout.pop, NULL,
FALSE);
Create Popupwindow instance, 200,150 width and height respectively
Popupwindow = new Popupwindow (Popupwindow_view, MB, true);
Set animation effects
Popupwindow.setanimationstyle (R.style.animationfade);
Click the other place to disappear
Popupwindow_view.setontouchlistener (New Ontouchlistener () {
@Override
public boolean Ontouch (View V, motionevent event) {
TODO auto-generated Method Stub
if (Popupwindow!= null && popupwindow.isshowing ()) {
Popupwindow.dismiss ();
Popupwindow = null;
}
return false;
}
});
Controls inside the Pop.xml view
Button open = (Button) Popupwindow_view.findviewbyid (R.id.open);
Button save = (Button) Popupwindow_view.findviewbyid (R.id.save);
Button close = (Button) Popupwindow_view.findviewbyid (r.id.close);
Events triggered by controls inside the Pop.xml view
Open it
Open.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Here you can do the related operations
System.out.println ("open operation");
The dialog box disappears
Popupwindow.dismiss ();
}
});
Save
Save.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Here you can do the related operations
System.out.println ("save operation");
Popupwindow.dismiss ();
}
});
Shut down
Close.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Here you can do the related operations
SYSTEM.OUT.PRINTLN ("close operation");
Popupwindow.dismiss ();
}
});
}
/***
* Get Popupwindow instance
*/
private void Getpopupwindow () {
if (null!= Popupwindow) {
Popupwindow.dismiss ();
Return
} else {
Initpopuptwindow ();
}
}
}

Main interface
2.main.xml
Copy Code code as follows:

<?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" >
<button android:id= "@+id/popbtn"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/pop_left"/>
</LinearLayout>

Layout of the pop-up layer
3.pop.xml
Copy Code code as follows:

<?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"
Android:background= "@android: Color/darker_gray" >
<button android:id= "@+id/open"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:background= "@drawable/btn"
android:text= "@string/open"/>
<button android:id= "@+id/save"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:background= "@drawable/btn"
android:text= "@string/save"/>
<button android:id= "@+id/close"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:background= "@drawable/btn"
android:text= "@string/close"/>
</LinearLayout>

The style file under value
4.style
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<style name= "Animationfade" >
<!--Popupwindow The effect of popping around-->
<item name= "Android:windowenteranimation" > @anim/in_lefttoright</item>
<item name= "Android:windowexitanimation" > @anim/out_righttoleft</item>
</style>
</resources>

String file under Value
5.string.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" >hello World, popwindowonleftactivity!</string>
<string name= "App_name" >PopwindowOnLeft</string>
<string name= "Pop_left" > Popup left Menu </string>
<string name= "Open" > Open </string>
<string name= "Save" > Save </string>
<string name= "Close" > Shutdown </string>
</resources>

Files in the Anim directory
Animated file from left to right when it appears
6.in_lefttoright.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--defines an animated--> that enters from left to right
<translate
Android:fromxdelta= "-100%"
Android:toxdelta= "0"
Android:duration= "/>"
</set>

Animation that disappears from right to left when exiting
7.out_righttoleft.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--definition Animation exit animation--> from right to left
<translate
Android:fromxdelta= "0"
Android:toxdelta= "-100%"
Android:duration= "/>"
</set>

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.