当前位置: 移动技术网 > 移动技术>移动开发>Android > android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍

android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍

2019年07月24日  | 移动技术网移动技术  | 我要评论

popupwindow可以实现浮层效果,主要方法有:可以自定义view,通过layoutinflator方法;可以出现和退出时显示动画;可以指定显示位置等。

为了将popupwindow的多个功能展现并力求用简单的代码实现,编写了一个点击按钮左侧弹出菜单的功能,实现出现和退出时显示动画效果并点击其他区域时弹出层自动消失,效果图如下:
源码:
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 {
// 声明popupwindow对象的引用
private popupwindow popupwindow;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
// 点击按钮弹出菜单
button pop = (button) findviewbyid(r.id.popbtn);
pop.setonclicklistener(popclick);
}
//点击弹出左侧菜单的显示方式
onclicklistener popclick = new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
getpopupwindow();
// 这里是位置显示方式,在按钮的左下角
popupwindow.showasdropdown(v);
// 这里可以尝试其它效果方式,如popupwindow.showasdropdown(v,
// (screenwidth-dialgowidth)/2, 0);
// popupwindow.showatlocation(findviewbyid(r.id.layout),
// gravity.center, 0, 0);
}
};
/**
* 创建popupwindow
*/
protected void initpopuptwindow() {
// todo auto-generated method stub
// 获取自定义布局文件pop.xml的视图
view popupwindow_view = getlayoutinflater().inflate(r.layout.pop, null,
false);
// 创建popupwindow实例,200,150分别是宽度和高度
popupwindow = new popupwindow(popupwindow_view, 200, 150, true);
// 设置动画效果
popupwindow.setanimationstyle(r.style.animationfade);
//点击其他地方消失
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视图里面的控件
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视图里面的控件触发的事件
// 打开
open.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
// 这里可以执行相关操作
system.out.println("打开操作");
// 对话框消失
popupwindow.dismiss();
}
});
// 保存
save.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
// 这里可以执行相关操作
system.out.println("保存操作");
popupwindow.dismiss();
}
});
// 关闭
close.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
// 这里可以执行相关操作
system.out.println("关闭操作");
popupwindow.dismiss();
}
});
}
/***
* 获取popupwindow实例
*/
private void getpopupwindow() {
if (null != popupwindow) {
popupwindow.dismiss();
return;
} else {
initpopuptwindow();
}
}
}

主要界面
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>

弹出层的布局
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>

value下的style文件
4.style
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="animationfade">
<!-- popupwindow左右弹出的效果-->
<item name="android:windowenteranimation">@anim/in_lefttoright</item>
<item name="android:windowexitanimation">@anim/out_righttoleft</item>
</style>
</resources>

value下的string文件
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">弹出左侧菜单</string>
<string name="open">打开</string>
<string name="save">保存</string>
<string name="close">关闭</string>
</resources>

anim目录下的文件
出现时从左往右的动画文件
6.in_lefttoright.xml
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定义从左向右进入的动画 -->
<translate
android:fromxdelta="-100%"
android:toxdelta="0"
android:duration="500"/>
</set>

退出时从右往左消失的动画
7.out_righttoleft.xml
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定义从右向左动画退出动画 -->
<translate
android:fromxdelta="0"
android:toxdelta="-100%"
android:duration="500"/>
</set>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网