<PopupWindow>的简单功能和用法
2016-06-05 14:27
281 查看
package com.crazyit.ui.popupwindowdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.PopupWindow; /** * PopupWindow 的简单功能和用法 */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //加载popup 对应的界面 View root = getLayoutInflater().inflate(R.layout.popup, null); //创建 PopupWindow 对象 final PopupWindow popupWindow = new PopupWindow(root, 280, 360); Button btn = (Button) findViewById(R.id.button); if (btn != null) { btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //以下拉方式显示 popupWindow.showAsDropDown(v); //方式1 显示在 popupwindow 的下方 //将 popupWindow显示在指定的位置 // popupWindow.showAtLocation(findViewById(R.id.button), Gravity.CENTER, 40, 20); } }); } root.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //关闭 popupWindow popupWindow.dismiss(); } }); } }
布局界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.crazyit.ui.popupwindowdemo.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="弹出Popup窗口" android:id="@+id/button" /> </RelativeLayout>
popupWindow 的布局界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.crazyit.ui.popupwindowdemo.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="弹出Popup窗口" android:id="@+id/button" /> </RelativeLayout>
使用popupWindow 创建对话框风格的窗口 需要以下2步
1. 调用 popupWindow的构造器创建PopupWindow对象
2. 调用 popupwindow的 showAsDropDown(View v) 将popupWindow作为v组件的下拉组件显示出来; 或者调用popupWindow 的showAtLocation() 方法将PopupWindow在指定的位置显示出来
相关文章推荐
- 四、运维自动化发展―Web化
- Nginx源码安装
- Android项目重构之路:架构篇
- Could not publish server configuration for Tomcat v7.0 Server at localhost.
- Zookeeper linux下集群部署
- 学技术的思路理念
- Linux软件包管理
- 三、运维自动化发展―工具化
- 新闻发布系统——网站发布
- 三层架构
- Linux内核线程kernel thread详解--Linux进程的管理与调度(十)
- Apache Tomcat 绿色版安装Service(服务)
- Nginx两种安装方式简述
- 一、运维学习和发展的一个线路
- 我的nginx配置文件
- Centos7 SPICE 源码编译安装(2)----- 编译spice-gtk-0.31.tar.bz2
- Centos7 SPICE 源码编译安装(1)-----编译spice-0.13.1.tar.bz2
- CentOS6.5系统下RPM包安装MySQL5.6
- centos 7 添加中文输入法
- 一个在线jpg png转ICO的网站