PopupWindow的使用简介

一、简介

PopupWindow一般用于View控件周边的弹出窗口。

public PopupWindow (View contentView, int width, int height, boolean focusable)

创建一个新的弹出窗口可以显示contentView。窗户的尺寸必须传递给构造函数。

二、使用

1.创建弹出窗口popup_widow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <RadioButton
         android:paddingLeft="10dp"
        android:paddingRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/str_hdmi14"
        android:textColor="@color/white"
        android:textSize="32sp" 
        android:button="@drawable/radio_btn"
        android:background="@drawable/xxx"/>
    
    <RadioButton
        android:paddingLeft="10dp"
        android:paddingRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/str_hdmi20"
        android:textColor="@color/white"
        android:textSize="32sp" 
        android:button="@drawable/radio_btn"
        android:background="@drawable/xxx"/>

</LinearLayout>

2.在java代码中用LayoutInflater加载该窗口布局文件

View rootView =  LayoutInflater.from(mContext).inflate(
		R.layout.popup_window, null);

3.创建PopupWindow对象

 PopupWindow popupWindow = new PopupWindow(rootView,300, 200, true);

-------------------------------------------------------------------------------------------------------------------------------

4.设置PopupWindow的动画风格(可选)

popupWindow.setAnimationStyle(R.style.popup_window_anim);

4.1在values/style.xml定义动画风格

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="popup_window_anim">
        <item name="android:windowEnterAnimation">@anim/popup_window_show</item>
        <item name="android:windowExitAnimation">@anim/popup_window_hide</item>
    </style>
</resources>

4.2 弹出动画 anim/pop_window_show.xml

<?xml version="1.0" encoding="utf-8"?>
<set  xmlns:android="http://schemas.android.com/apk/res/android">
	<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
	          android:fromXScale="1.0"
	          android:toXScale="1.0"
	          android:fromYScale="0.0"
	          android:toYScale="1.0"
	          android:fillAfter="false"
	          android:duration="200">
	</scale>
</set>

4.3消失动画 anim/pop_window_hide.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
              android:fromXScale="1.0"
              android:toXScale="1.0"
              android:fromYScale="1.0"
              android:toYScale="0.0"
              android:fillAfter="false"
              android:duration="200">
    </scale>
</set>

-------------------------------------------------------------------------------------------------------------------------------

5.显示PopupWindow

 popupWindow.showAsDropDown(btn ,xOff ,yOff);

参数:

btn: 在btn这个View控件周边弹出

xOff:x轴的偏移

yOff:y轴的偏移

注:如xOff = 0; yOff=0;则窗口在btn正下弹出(左边缘对齐)

-------------------------------------------------------------------------------------------------------------------------------

时间: 2024-08-07 20:07:02

PopupWindow的使用简介的相关文章

PopupWindow(1)简介

PopupWindow有点类似于Dialog,相同点在于都是弹出窗口,并且都可以对其进行自定义显示,并且里面的监听组件,进行相应的操作,但它与Dialog又有很大的区别,PopupWindow只是弹出窗口,不会使宿主Activity组件失去焦点,也就是说PopupWindow弹出后,你仍可以与宿主Activity进行交互,Dialog却不能做到这一点. 常见popupwindow效果图:

PopupWindow 动画

1.PopupWindow 简介 首先看android.widget.PopupWindow.java源码注释: /** * <p>A popup window that can be used to display an arbitrary view. The popup * window is a floating container that appears on top of the current * activity.</p> * * @see android.widg

【Android 应用开发】 ActionBar 样式详解 -- 样式 主题 简介 Actionbar 的 icon logo 标题 菜单样式修改

作者 : 万境绝尘 ([email protected]) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/39269163 示例代码下载 : -- GitHub : https://github.com/han1202012/Octopus_ActionBarStyle.git -- CSDN : http://download.csdn.net/detail/han1202012/7926959 一. 样式 和 主题 资源

PopupWindow选项弹窗

PopupWindow选项弹窗 一.popupwindow弹窗简介 popupwindow是一种弹窗在应用中经常使用,像超级课程表的周数选择,微博的标题栏的titlebar中的选择等等 ,如图所示. 但是和alertDialog很相似,但是还是有很大的区别的.AlterDialog是非阻塞式对话框:AlaterDialog弹出时,后台还可以做事情:而且PopuWindow是阻塞式对话框:PopupWindow弹出时,后台程序会等待,在PopupWindow退出之前一直处于等待,直到当我们调用了d

使用android.graphics.Path类自绘制PopupWindow背景

PopupWindow简介 PopupWindow是悬浮在当前activity上的一个容器,用它可以展示任意的内容. PopupWindow跟位置有关的API有下面几个: showAsDropDown(View anchor, int xoff, int yoff, int gravity) 显示在anchor的左下角,通过xoff,yoff调整距离,gravity是popup相对于anchor的对齐方式.如果popup超出屏幕,并且展示内容的根容器是滑动控件,将以滑动方式展示.如果展示内容根容

ActionBar 样式详解 -- 样式 主题 简介

1. 样式资源解析 (1) 样式简介 样式解析 : 样式是设置给 View 组件的多个属性的集合; -- 样式的好处 : 给一个 TextView 设置 文字大小, 颜色, 对齐方式等, 如果需要给多个 TextView 设置同样的属性, 这里使用样式就可以大大节省了代码量; 样式指定 : 每个 View 组件都有一个 style 属性, 可以通过该属性指定 样式, 注意与 android:style 属性区分; (2) 样式标签定义 样式定义的位置 : /res/values/ 目录下的 跟标

Android的PopupWindow使用android学习之旅(四十三)

PopupWindow简介 PopupWindow是一个类似dialog的控件,可以接受任何的view作为下拉列表显示. 用法 代码展示 package peng.liu.test; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.Gravity; im

PopupWindow 使用详解

极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容: PopupWindow 继承关系 PopupWindow 使用方法 PopupWindow 底部PopupWindow的实现 PopupWindow 是一个可以在Activity之上显示任意View的控件.在Android经常使用,效果跟Dialog效果类似,不同点在于可以控制显示的位置,

Android网络通讯简介

网络通信应该包含三部分的内容:发送方.接收方.协议栈.发送方和接收方是参与通信的主体,协议栈是发送方和接收方进行通信的契约.按照服务类型,网络通信可分为面向连接和无连接的方式.面向连接是在通信前建立通信链路,而通信结束后释放该链路.无连接的方式则不需要在通信前建立通信连接,这种方式不保证传输的质量. Android提供了多种网络通信的方式,如Java中提供的网络编程,在Android中都提供了支持.Android中常用的网络编程方式如下: 针对TCP/IP协议的Socket和ServerSock