android popwindow implementation of the left popup menu layer and PopupWindow main methods
- 2020-05-09 19:18:37
- OfStack
PopupWindow can achieve the floating layer effect, the main methods are: you can customize view, through the LayoutInflator method; Can appear and exit when the display animation; You can specify display location, etc.
In order to display multiple functions of PopupWindow and try to achieve them with simple code, a function of clicking the left side of the button to pop up the menu was programmed. The animation effect was displayed when appearing and exiting, and the popup layer automatically disappeared when clicking other areas. The effect picture is as follows:
Source:
1.PopwindowOnLeftActivity.java
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 {
// The statement PopupWindow Object reference
private PopupWindow popupWindow;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Click the button to bring up the menu
Button pop = (Button) findViewById(R.id.popBtn);
pop.setOnClickListener(popClick);
}
// Click to display the menu on the left
OnClickListener popClick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getPopupWindow();
// Here's how the location is displayed , It's in the lower left corner of the button
popupWindow.showAsDropDown(v);
// You can try other effects here , 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
// Gets the custom layout file pop.xml The view of
View popupWindow_view = getLayoutInflater().inflate(R.layout.pop, null,
false);
// create PopupWindow The instance ,200,150 Width and height, respectively
popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);
// Set the animation effect
popupWindow.setAnimationStyle(R.style.AnimationFade);
// Click elsewhere 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;
}
});
// pop.xml The control inside the 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);
// pop.xml An event triggered by a control in a view
// Open the
open.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// You can do this here
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
// You can do this here
System.out.println(" The save operation ");
popupWindow.dismiss();
}
});
// Shut down
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// You can do this here
System.out.println(" closing ");
popupWindow.dismiss();
}
});
}
/***
* To obtain PopupWindow The instance
*/
private void getPopupWindow() {
if (null != popupWindow) {
popupWindow.dismiss();
return;
} else {
initPopuptWindow();
}
}
}
The main interface
2.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" >
<Button android:id="@+id/popBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pop_left" />
</LinearLayout>
Pop up the layout of the layer
3.pop.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"
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>
style file under value
4.style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AnimationFade">
<!-- PopupWindow Left and right popup effects -->
<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
<?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"> Left menu pops up </string>
<string name="open"> Open the </string>
<string name="save"> save </string>
<string name="close"> Shut down </string>
</resources>
Files in the anim directory
An animation file that appears from left to right
6.in_lefttoright.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Defines an animation that enters from left to right -->
<translate
android:fromXDelta="-100%"
android:toXDelta="0"
android:duration="500"/>
</set>
An animation that disappears from right to left when exiting
7.out_righttoleft.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Defines the exit from the animation from right to left -->
<translate
android:fromXDelta="0"
android:toXDelta="-100%"
android:duration="500"/>
</set>