【Android】创建Popwindow弹出菜单的两种方式
16lz
2022-07-28
方法一的Activity
[java] view plain copy- packagecom.app.test02;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.Gravity;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.view.View.OnTouchListener;
- importandroid.view.ViewGroup.LayoutParams;
- importandroid.widget.Button;
- importandroid.widget.PopupWindow;
- importandroid.widget.Toast;
- publicclassPopwindowLeftextendsActivity{
- //声明PopupWindow对象的引用
- privatePopupWindowpopupWindow;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_popupwindow_main);
- //点击按钮弹出菜单
- Buttonpop=(Button)findViewById(R.id.popBtn);
- pop.setOnClickListener(popClick);
- }
- //点击弹出左侧菜单的显示方式
- OnClickListenerpopClick=newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- getPopupWindow();
- //这里是位置显示方式,在屏幕的左侧
- popupWindow.showAtLocation(v,Gravity.LEFT,0,0);
- }
- };
- /**
- *创建PopupWindow
- */
- protectedvoidinitPopuptWindow(){
- //TODOAuto-generatedmethodstub
- //获取自定义布局文件activity_popupwindow_left.xml的视图
- ViewpopupWindow_view=getLayoutInflater().inflate(R.layout.activity_popupwindow_left,null,
- false);
- //创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
- popupWindow=newPopupWindow(popupWindow_view,200,LayoutParams.MATCH_PARENT,true);
- //设置动画效果
- popupWindow.setAnimationStyle(R.style.AnimationFade);
- //点击其他地方消失
- popupWindow_view.setOnTouchListener(newOnTouchListener(){
- @Override
- publicbooleanonTouch(Viewv,MotionEventevent){
- //TODOAuto-generatedmethodstub
- if(popupWindow!=null&&popupWindow.isShowing()){
- popupWindow.dismiss();
- popupWindow=null;
- }
- returnfalse;
- }
- });
- }
- /***
- *获取PopupWindow实例
- */
- privatevoidgetPopupWindow(){
- if(null!=popupWindow){
- popupWindow.dismiss();
- return;
- }else{
- initPopuptWindow();
- }
- }
- }
方法二的Activity
[java] view plain copy- packagecom.app.test02;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.Gravity;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.view.View.OnTouchListener;
- importandroid.view.ViewGroup.LayoutParams;
- importandroid.widget.PopupWindow;
- publicclassPopwindowLeftNewextendsActivity{
- privatePopupWindowpopupWindow;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_popupwindow_main);
- findViewById(R.id.popBtn).setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- //获取自定义布局文件activity_popupwindow_left.xml的视图
- ViewpopupWindow_view=getLayoutInflater().inflate(R.layout.activity_popupwindow_left,null,false);
- //创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
- popupWindow=newPopupWindow(popupWindow_view,200,LayoutParams.MATCH_PARENT,true);
- //设置动画效果
- popupWindow.setAnimationStyle(R.style.AnimationFade);
- //这里是位置显示方式,在屏幕的左侧
- popupWindow.showAtLocation(v,Gravity.LEFT,0,0);
- //点击其他地方消失
- popupWindow_view.setOnTouchListener(newOnTouchListener(){
- @Override
- publicbooleanonTouch(Viewv,MotionEventevent){
- //TODOAuto-generatedmethodstub
- if(popupWindow!=null&&popupWindow.isShowing()){
- popupWindow.dismiss();
- popupWindow=null;
- }
- returnfalse;
- }
- });
- }
- });
- }
- }
效果图
附:一些相关的布局文件
PopupWindow弹出菜单
activity_popupwindow_main.xml[html] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#fff">
- <Buttonandroid:id="@+id/popBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="弹出左侧菜单"/>
- </LinearLayout>
activity_popupwindow_left.xml
[html] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@android:color/darker_gray"
- android:orientation="vertical"
- android:gravity="center"
- android:paddingTop="50dp">
- <Button
- android:id="@+id/open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="打开"/>
- <Button
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="保存"/>
- <Button
- android:id="@+id/close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="关闭"/>
- <Button
- android:id="@+id/open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="打开"/>
- <Button
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="保存"/>
- <Button
- android:id="@+id/close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="关闭"/>
- <Button
- android:id="@+id/open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="打开"/>
- <Button
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="保存"/>
- <Button
- android:id="@+id/close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="@android:color/darker_gray"
- android:text="关闭"/>
- </LinearLayout>
弹出动画XML
在res文件夹下,建立anim文件夹。写入如下两个文件。弹出动画
in_lefttoright.xml[html] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <setxmlns:android="http://schemas.android.com/apk/res/android">
- <!--定义从左向右进入的动画-->
- <translate
- android:duration="500"
- android:fromXDelta="-100%"
- android:toXDelta="0"/>
- </set>
弹回动画
out_righttoleft.xml[html] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <setxmlns:android="http://schemas.android.com/apk/res/android">
- <!--定义从右向左动画退出动画-->
- <translate
- android:duration="500"
- android:fromXDelta="0"
- android:toXDelta="-100%"/>
- </set>
动画管理
在styles.xml中,添加如下管理代码。 [html] view plain copy- <stylename="AnimationFade">
- <!--PopupWindow左右弹出的效果-->
- <itemname="android:windowEnterAnimation">@anim/in_lefttoright</item>
- <itemname="android:windowExitAnimation">@anim/out_righttoleft</item>
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- 读取android手机流量信息
- Android(安卓)Activity界面切换添加动画特效
- android 使用html5作布局文件: webview跟javascript交互
- android实现字体闪烁动画的方法
- Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
- android“设置”里的版本号
- Android开发环境搭建