关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » 安卓开发 »Android中PopupWindow使用详解

    Android中PopupWindow使用详解

    2014-09-13 00:00:00 出处:生命壹号的博客
    分享

    PopupWindow是应用开发中经常用到的组建,使用它可以在当前屏幕的上层显示一个弹窗,同时也可以指定弹窗的位置以及背景色等特性,大大提高用户体验,那么这里我就以下几点介绍它的使用:

    1 从指定的位置弹出这个窗口(淡入淡出动画)

    2 从屏幕底部弹出这个窗口(带有透明度背景,自定义触摸其他位置自动关闭弹窗)

    我的效果图如下:

    下面直接上代码,具体如下所示(按开发顺序排列)

    1 自定义一个继承自PopupWindow的类

    publicclassPopupDialog extendsPopupWindow {

    publicPopupDialog(view view,intwidth,intheight) {

    super(view,width,height);

    }

    }

    2、自定义一个负责设定PopupWindow特性的属性类

    publicclassPopup {

    privateintxPos; //弹出窗口的x方向位置

    privateintyPos; //弹出窗口的y方向位置

    privateintvWidth; //窗口显示内容的视图宽度

    privateintvHeight; //窗口显示内容的视图高度

    privateintanimFadeInOut; //窗口显示动画

    privateintcontentView; //潜入在窗口的视图

    privateView customView; //潜入的窗口视图view

    privatebooleanisClickable; //视图外部是否可以点击

    privateOnDismissListener listener; //监听弹窗是否dismiss

    privateOnTouchListener touchListener; //监听触摸位置

    privatefloatbgAlpha; //背景遮罩的透明度

    publicintgetxPos() {

    returnxPos;

    }

    publicvoidsetxPos(intxPos) {

    this.xPos= xPos;

    }

    publicintgetyPos() {

    returnyPos;

    }

    publicvoidsetyPos(intypos) {

    this.yPos= ypos;

    }

    publicintgetvWidth() {

    returnvWidth;

    }

    publicvoidsetvWidth(intvWidth) {

    this.vWidth= vWidth;

    }

    publicintgetvHeight() {

    returnvHeight;

    }

    publicvoidsetvHeight(intvHeight) {

    this.vHeight= vHeight;

    }

    publicintgetAnimFadeInOut() {

    returnanimFadeInOut;

    }

    publicvoidsetAnimFadeInOut(intanimFadeInOut){

    this.animFadeInOut= animFadeInOut;

    }

    publicintgetContentView() {

    returncontentView;

    }

    publicvoidsetContentView(intcontentView){

    this.contentView= contentView;

    }

    publicbooleanisClickable() {

    returnisClickable;

    }

    publicvoidsetClickable(booleanisClickable){

    this.isClickable= isClickable;

    }

    publicView getCustomView() {

    returncustomView;

    }

    publicvoidsetCustomView(View customView){

    this.customView= customView;

    }

    publicOnDismissListener getListener() {

    returnlistener;

    }

    publicvoidsetListener(OnDismissListener listener){

    this.listener= listener;

    }

    publicfloatgetBgAlpha() {

    returnbgAlpha;

    }

    publicvoidsetBgAlpha(floatbgAlpha) {

    this.bgAlpha= bgAlpha;

    }

    publicOnTouchListener getTouchListener() {

    returntouchListener;

    }

    publicvoidsetTouchListener(OnTouchListener touchListener){

    this.touchListener= touchListener;

    }

    3 、自定义一个用来管理PopupWindow的工具类

    publicclassPopupUtils {

    privatestaticPopupDialog popupDialog= null;

    @SuppressLint(“NewApi”)

    publicstaticPopupDialog createPopupDialog(Context context,Popupdialog) {

    dismissPopupDialog();

    Viewview =null;

    if(ValueUtils.isEmpty(dialog.getCustomView())){

    LayoutInflaterinflater =LayoutInflater.from(context);

    view= inflater.inflate(dialog.getContentView(),null);

    }else{

    view= dialog.getCustomView();

    }

    view.setOnTouchListener(dialog.getTouchListener());

    if(0!= dialog.getBgAlpha()){

    view.setAlpha(dialog.getBgAlpha());

    }

    popupDialog= newPopupDialog(view,dialog.getvWidth(),dialog.getvHeight());

    ColorDrawabledw = newColorDrawable(Color.TRANSPARENT); //follow two lines is used for back key -00000

    popupDialog.setBackgroundDrawable(dw);

    popupDialog.setAnimationStyle(dialog.getAnimFadeInOut());

    popupDialog.setOutsideTouchable(dialog.isClickable());

    popupDialog.setFocusable(true); //not allow user click popupwindowbackground event or not permit

    popupDialog.setOnDismissListener(dialog.getListener());

    popupDialog.update();

    returnpopupDialog;

    }

    publicstaticvoiddismissPopupDialog() {

    if(ValueUtils.isNotEmpty(popupDialog)&&

    popupDialog.isShowing()){

    popupDialog.dismiss();

    popupDialog= null;

    }

    }

    publicstaticbooleanisPopupShowing() {

    if(ValueUtils.isNotEmpty(popupDialog)&&

    popupDialog.isShowing()){

    returntrue;

    }else{

    returnfalse;

    }

    }

    }

    接下来,我们需要准备相关的资源文件了,比如:导航按钮页面,采购清单弹窗页面以,选择美图窗口页面以及需要用到的动画文件和圆角背景文件,具体如下:

    导航按钮页面xml:

    <ScrollViewxmlns:android=“http://schemas.android.com/apk/res/android”

    android:layout_width=“match_parent”

    android:layout_height=“match_parent”

    android:id=“@+id/main”

    android:background=“#f2f3f4″>

    <LinearLayout

    android:layout_width=“match_parent”

    android:layout_height=“wrap_content”

    android:orientation=“vertical”>

    <Button

    android:id=“@+id/btnDropDownPopupDialog”

    android:layout_width=“match_parent”

    android:layout_height=“40dp”

    android:layout_gravity=“center_horizontal”

    android:layout_marginLeft=“10dp”

    android:layout_marginRight=“10dp”

    android:layout_marginTop=“10dp”

    android:background=“#FFFFFF”

    android:textColor=“#5084FE”

    android:text=“采购清单“

    />

    <Button

    android:id=“@+id/btnDownUpPopupDialog”

    android:layout_width=“match_parent”

    android:layout_height=“40dp”

    android:layout_gravity=“center_horizontal”

    android:layout_marginLeft=“10dp”

    android:layout_marginRight=“10dp”

    android:layout_marginTop=“10dp”

    android:background=“#FFFFFF”

    android:textColor=“#5084FE”

    android:text=“选择美图“

    />

    </LinearLayout>

    </ScrollView>

    采购清单弹窗页面xml:

    <RelativeLayoutxmlns:android=“http://schemas.android.com/apk/res/android”

    android:layout_width=“wrap_content”

    android:layout_height=“wrap_content”

    android:background=“#c9cbce”

    android:gravity=“center”

    >

    <TextView

    android:id=“@+id/tvbillTypeYc”

    android:layout_width=“match_parent”

    android:layout_height=“wrap_content”

    android:text=“粤菜“

    android:textColor=“#5084FE”

    android:padding=“10dp”

    android:layout_margin=“10dp”

    android:gravity=“center”

    android:background=“@drawable/corners_bk_gray2″

    />

    <TextView

    android:id=“@+id/tvbillTypeCc”

    android:layout_width=“match_parent”

    android:layout_height=“wrap_content”

    android:text=“川菜“

    android:textColor=“#5084FE”

    android:padding=“10dp”

    android:layout_margin=“10dp”

    android:gravity=“center”

    android:layout_below=“@id/tvbillTypeYc”

    android:background=“@drawable/corners_bk_gray2″

    />

    <TextView

    android:id=“@+id/tvbillTypeXc”

    android:layout_width=“match_parent”

    android:layout_height=“wrap_content”

    android:text=“湘菜“

    android:textColor=“#5084FE”

    android:padding=“10dp”

    android:layout_margin=“10dp”

    android:gravity=“center”

    android:layout_below=“@id/tvbillTypeCc”

    android:background=“@drawable/corners_bk_gray2″

    />

    <TextView

    android:id=“@+id/tvbillTypeMore”

    android:layout_width=“match_parent”

    android:layout_height=“wrap_content”

    android:text=“…”

    android:textColor=“#5084FE”

    android:padding=“10dp”

    android:layout_margin=“10dp”

    android:gravity=“center”

    android:layout_below=“@id/tvbillTypeXc”

    android:background=“@drawable/corners_bk_gray2″

    />

    </RelativeLayout>

    选择美图弹窗页面xml:

    <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

    android:layout_width=“match_parent”

    android:layout_height=“match_parent”

    android:background=“#00000000″

    >

    <FrameLayout

    android:id=“@+id/flMaskLayer”

    android:layout_width=“match_parent”

    android:layout_height=“match_parent”

    android:background=“#000000″

    />

    <LinearLayout

    android:id=“@+id/llHeader”

    android:layout_width=“match_parent”

    android:layout_height=“wrap_content”

    android:layout_marginLeft=“10dp”

    android:layout_marginRight=“10dp”

    android:orientation=“vertical”

    android:layout_alignParentBottom=“true”

    >

    <RelativeLayout

    android:layout_width=“match_parent”

    android:layout_height=“81.0dp”

    android:background=“@drawable/corners_bk_white”

    >

    <TextView

    android:id=“@+id/tvTakeHeader”

    android:layout_width=“match_parent”

    android:layout_height=“40dp”

    android:text=“拍照“

    android:textColor=“#5084FE”

    android:gravity=“center”

    />

    <View

    android:layout_width=“match_parent”

    android:layout_height=“1dp”

    android:background=“@color/gray”

    android:layout_centerVertical=“true”

    />

    <TextView

    android:id=“@+id/tvHeaderFromSD”

    android:layout_width=“match_parent”

    android:layout_height=“40dp”

    android:text=“从相册中选“

    android:textColor=“#5084FE”

    android:gravity=“center”

    android:layout_below=“@id/tvTakeHeader”

    />

    </RelativeLayout>

    <TextView

    android:id=“@+id/tvCancel”

    android:layout_width=“match_parent”

    android:layout_height=“40dp”

    android:text=“取消“

    android:textColor=“#5084FE”

    android:background=“@drawable/corners_bk_white”

    android:gravity=“center”

    android:layout_marginTop=“18dp”

    />

    </LinearLayout>

    </RelativeLayout>

    淡入淡出动画文件xml:

    淡入动画:

    < xmlversion=“1.0″encoding=“utf-8″standalone=“no” >

    <setxmlns:android=“http://schemas.android.com/apk/res/android”

    android:interpolator=“@android:anim/decelerate_interpolator”>

    <alpha

    android:duration=“500″

    android:fromAlpha=“0.0″

    android:toAlpha=“1.0″/>

    </set>

    淡出动画:

    < xmlversion=“1.0″encoding=“utf-8″standalone=“no” >

    <setxmlns:android=“http://schemas.android.com/apk/res/android”

    android:interpolator=“@android:anim/decelerate_interpolator”>

    <alpha

    android:duration=“300″

    android:fromAlpha=“1.0″

    android:toAlpha=“0.0″/>

    </set>

    圆角背景文件xml:

    采购清单背景:

    < xmlversion=“1.0″encoding=“utf-8″ >

    <shapexmlns:android=“http://schemas.android.com/apk/res/android”>

    <solidandroid:color=“#efece0″/>

    <corners

    android:radius=“4dp”/>

    <padding

    android:bottom=“5dp”

    android:left=“10dp”

    android:right=“10dp”

    android:top=“5dp”/>

    <stroke

    android:width=“1dp”

    android:color=“#efece0″/>

    </shape>

    底部弹窗:

    < xmlversion=“1.0″encoding=“utf-8″ >

    <shapexmlns:android=“http://schemas.android.com/apk/res/android”>

    <solidandroid:color=“#FFFFFF”/>

    <corners

    android:bottomLeftRadius=“6dp”

    android:bottomRightRadius=“6dp”

    android:topLeftRadius=“6dp”

    android:topRightRadius=“6dp”/>

    </shape>

    到这里,我们已经准备了所有需要的工作,接下来就是在我们的前段页面中调用即可。主要是通过参数对象包装参数,并调用上面的工具类进行展示和隐藏我们需要的PopupWindow即可,具体如下:

    publicclassViewUtilsActivity extendsFragmentActivity {

    privatestaticViewUtilsFragment fragment= null;

    @Override

    protectedvoidonCreate(Bundle bundle){

    super.onCreate(bundle);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_base_main);

    if(null== bundle){

    FragmentTransactionft =getSupportFragmentManager().beginTransaction();

    fragment= newViewUtilsFragment();

    ft.add(R.id.container,fragment);

    ft.commit();

    }

    }

    publicclassViewUtilsFragment extendsFragment implementsView.OnClickListener {

    privatePopupDialog popupDialog= null;

    privateButton btnDropDownPopupDialog= null;

    privateButton btnDownUpPopupDialog= null;

    @Override

    publicView onCreateView(LayoutInflater inflater,ViewGroup container,

    BundlesavedInstanceState){

    ViewrootView =(View) inflater.inflate(R.layout.fragment_viewutils,container,false);

    btnDropDownPopupDialog= (Button)rootView.findViewById(R.id.btnDropDownPopupDialog);

    btnDropDownPopupDialog.setOnClickListener(this);

    btnDownUpPopupDialog= (Button) rootView.findViewById(R.id.btnDownUpPopupDialog);

    btnDownUpPopupDialog.setOnClickListener(this);

    returnrootView;

    }

    @Override

    publicvoidonClick(View v){

    switch(v.getId()){

    caseR.id.btnDropDownPopupDialog:

    showDropDownPopupDialog();

    break;

    caseR.id.btnDownUpPopupDialog:

    showDownUpPopupDialog();

    break;

    default:

    break;

    }

    }

    privatevoidshowDropDownPopupDialog() {

    Popuppopup = newPopup();

    //这里是获得屏幕宽度使弹窗水平居中

    intxPos =(AppUtils.getScreenWidth(getActivity())- btnDropDownPopupDialog.getWidth())/ 2;

    popup.setxPos(xPos);

    popup.setyPos(0);

    popup.setvWidth(LayoutParams.WRAP_CONTENT);

    popup.setvHeight(LayoutParams.WRAP_CONTENT);

    popup.setClickable(true);

    popup.setAnimFadeInOut(R.style.AnimationFade);

    popup.setContentView(R.layout.view_popup_dialog);

    popupDialog= ViewUtils.createPopupDialog(getActivity(),popup);

    popupDialog.showAsDropDown(btnDropDownPopupDialog,popup.getxPos(),popup.getyPos());

    }

    @SuppressLint({“NewApi”,”ClickableViewAccessibility”})

    privatevoidshowDownUpPopupDialog() {

    Popuppopup = newPopup();

    popup.setvWidth(LayoutParams.MATCH_PARENT);

    popup.setvHeight(LayoutParams.MATCH_PARENT);

    popup.setClickable(true);

    popup.setContentView(R.layout.view_userheader_modifydetail);

    //设置触摸其他位置时关闭窗口

    OnTouchListenerlistener =newOnTouchListener() {

    @Override

    publicbooleanonTouch(View view,MotionEvent event){

    intheight =view.findViewById(R.id.llHeader).getTop();

    inty = (int)event.getY();

    if(event.getAction()==MotionEvent.ACTION_UP){

    if(y<height){

    ViewUtils.dismissPopupDialog();

    }

    }

    returntrue;

    }

    };

    popup.setTouchListener(listener);

    popupDialog= ViewUtils.createPopupDialog(getActivity(),popup);

    popupDialog.showAtLocation(getActivity().findViewById(R.id.main),

    Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,popup.getxPos(),popup.getyPos());

    Viewview =popupDialog.getContentView();

    //背景透明度设置

    view.findViewById(R.id.flMaskLayer).setAlpha(0.75f);

    View.OnClickListenerl = newView.OnClickListener() {

    @Override

    publicvoidonClick(View v){

    if(v.getId()== R.id.tvCancel){

    ViewUtils.dismissPopupDialog();

    }

    elseif(v.getId()== R.id.tvTakeHeader){

    //take phone

    Toast.makeText(getActivity(),”拍照”,Toast.LENGTH_SHORT).show();

    ViewUtils.dismissPopupDialog();

    }

    elseif(v.getId()== R.id.tvHeaderFromSD){

    //pick picture from sd

    Toast.makeText(getActivity(),”从相册中选”,Toast.LENGTH_SHORT).show();

    ViewUtils.dismissPopupDialog();

    }

    }

    };

    view.findViewById(R.id.tvCancel).setOnClickListener(l);

    view.findViewById(R.id.tvTakeHeader).setOnClickListener(l);

    view.findViewById(R.id.tvHeaderFromSD).setOnClickListener(l);

    }

    publicPopupDialog getPopupDialog() {

    returnpopupDialog;

    }

    }

    //监听返回按钮

    @Override

    publicbooleanonKeyDown(intkeyCode,KeyEvent event){

    switch(keyCode){

    caseKeyEvent.KEYCODE_BACK: {

    booleanflag =ViewUtils.isPopupShowing();

    if(flag){

    ViewUtils.dismissPopupDialog();

    returnfalse;

    }

    }

    break;

    default:

    break;

    }

    returnsuper.onKeyDown(keyCode,event);

    }

    好了,到这里我们的工作就完成啦,可以欣赏自己的作品了。

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    电脑屏幕不小心竖起来了?别慌,快捷键搞定

    Destoon 模板存放规则及语法参考

    Destoon系统常量与变量

    Destoon系统目录文件结构说明

    Destoon 系统安装指南

    Destoon会员公司主页模板风格添加方法

    Destoon 二次开发入门

    Microsoft 将于 2026 年 10 月终止对 Windows 11 SE 的支持

    Windows 11 存储感知如何设置?了解Windows 11 存储感知开启的好处

    Windows 11 24H2 更新灾难:系统升级了,SSD固态盘不见了...

    IT头条

    Synology 更新 ActiveProtect Manager 1.1 以增强企业网络弹性和合规性

    00:43

    新的 Rubrik Agent Cloud 加速了可信的企业 AI 代理部署

    00:34

    宇树科技 G1人形机器人,拉动一辆重达1.4吨的汽车

    00:21

    Cloudera 调查发现,96% 的企业已将 AI 集成到核心业务流程中,这表明 AI 已从竞争优势转变为强制性实践

    02:05

    投资者反对马斯克 1 万亿美元薪酬方案,要求重组特斯拉董事会

    01:18

    技术热点

    大型网站的 HTTPS 实践(三):基于协议和配置的优化

    ubuntu下右键菜单添加新建word、excel文档等快捷方式

    Sublime Text 简明教程

    用户定义SQL Server函数的描述

    怎么在windows 7开始菜单中添加下载选项?

    SQL Server 2016将有哪些功能改进?

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。