Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)
PopupWindow是一个可以用来显示一个任意的视图的弹出窗口,他需要完全依赖layout布局。
它没什么界面,在弹出的窗口中完全显示布局中的控件。

上面两个美女头就是弹窗PopupWindow显示的内容。是两个Button。
具体实现:
注意:那三个Button不能和普通的Button一样通过findViewById()方法获得,必须首先说的Button所在的视图,View popview = layoutInflater.inflate(R.layout.poplayout, null);
我的Button在poplayout.xml中。最后通过button1 = (Button) popview.findViewById(R.id.button1)获得。
另外一点就是:不要在oncreate()中获得Button,而是像我一样在onclick方法下获得,和popupwindow一起。这一点不一定正确。
为什么要提呢?因为我在oncreate()中获得Button时,button的点击事件不能用,我也不是很清楚。那位大牛要是知道的话,可以告诉我一下。
showAtLocation(findViewById(R.id.edit_layout), Gravity.BOTTOM,0, 0);
设置弹窗的位置:
第一个参数是弹窗父控件的布局;
第二个参数是位置如左,右,上部,下部等;
第三个参数是X方向的偏移量;
第四个参数是Y方向的偏移量。
- package xiaosi.popwindow;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.WindowManager.LayoutParams;
- import android.widget.Button;
- import android.widget.PopupWindow;
- import android.widget.Toast;
- public class PopwindowActivity extends Activity implements OnClickListener
- {
- /** Called when the activity is first created. */
- private Button button = null;
- private Button button1 = null;
- private Button button2 = null;
- private Button button3 = null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button) findViewById(R.id.button);
- button.setOnClickListener(this);
- }
- public void onClick(View arg0)
- {
- if (arg0.getId() == R.id.button)
- {
- LayoutInflater layoutInflater = (LayoutInflater) (PopwindowActivity.this)
- .getSystemService(LAYOUT_INFLATER_SERVICE);
- // 获取自定义布局文件poplayout.xml的视图
- View popview = layoutInflater.inflate(R.layout.poplayout, null);
- PopupWindow popWindow = new PopupWindow(popview,
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- //规定弹窗的位置
- popWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM,
- 0, 0);
- //PopupWindow里的两个Button
- button1 = (Button) popview.findViewById(R.id.button1);
- button1.setOnClickListener(this);
- button2 = (Button) popview.findViewById(R.id.button2);
- button2.setOnClickListener(this);
- }
- else if (arg0.getId() == R.id.button1)
- {
- Toast.makeText(PopwindowActivity.this, "button1", Toast.LENGTH_LONG)
- .show();
- }
- else if (arg0.getId() == R.id.button2)
- {
- Toast.makeText(PopwindowActivity.this, "button2", Toast.LENGTH_LONG)
- .show();
- }
- }
- }
poplayout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/colorframe_1" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/colorframe_3" />
- </LinearLayout>
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="@drawable/background">
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="点击查看效果" />
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/a" />
- </LinearLayout>
以上只是用来学习之用,拿出来和大家一起分享一下。
有想要源代码的给我留言。
http://blog.csdn.net/sjf0115/article/details/7339914
Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)的更多相关文章
- 【Android - 进阶】之PopupWindow的使用
创建一个类继承自PopupWindow,编写自定义的PopupWindow类.示例代码如下: import android.app.Activity; import android.graphics. ...
- JavaScrip——练习(做悬浮框进一步:悬浮窗后缀悬浮窗【感觉这种方法比较麻烦】)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android开发 PopupWindow弹窗调用第三方地图(百度,高德)实现导航功能
博客描述:后台返回地点的经纬度在地图上进行描点,点击导航弹出PopupWindow进行选择地图操作,如果手机中没有安装地图,提示没有,否则传值调起地图进行导航操作 看一下实现的效果,没图说再多都白搭 ...
- Android悬浮窗实现 使用WindowManager
Android悬浮窗实现 使用WindowManager WindowManager介绍 通过Context.getSystemService(Context.WINDOW_SERVICE)可以获得 ...
- 我的Android进阶之旅------>经典的大牛博客推荐(排名不分先后)!!
本文来自:http://blog.csdn.net/ouyang_peng/article/details/11358405 今天看到一篇文章,收藏了很多大牛的博客,在这里分享一下 谦虚的天下 柳志超 ...
- Android 使用WindowManager实现Android悬浮窗
WindowManager介绍 通过Context.getSystemService(Context.WINDOW_SERVICE)可以获得 WindowManager对象. 每一个WindowMan ...
- Android悬浮框,在Service中打开悬浮窗;在Service中打开Dialog;
文章介绍了如何在Service中显示悬浮框,在Service中弹出Dialog,在Service中做耗时的轮询操作: 背景需求: 公司的项目现在的逻辑是这样的:发送一个指令,然后3秒一次轮询去查询这个 ...
- Android无需权限显示悬浮窗, 兼谈逆向分析app
前言 最近UC浏览器中文版出了一个快速搜索的功能, 在使用其他app的时候, 如果复制了一些内容, 屏幕顶部会弹一个窗口, 提示一些操作, 点击后跳转到UC, 显示这个悬浮窗不需要申请android. ...
- (转载)Android UI设计之AlertDialog弹窗控件
Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...
随机推荐
- 【2013长沙区域赛】部分题解 hdu4791—4801
1001: 签到题,二分一下即可 代码: #include <set> #include <map> #include <cmath> #include <c ...
- Linq 语法举例
1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; // ...
- SOAP详解
1. SOAP简介 1.1应用背景 对于应用程序开发来说,使程序之间进行因特网通信是很重要的.目前的应用程序通过使用远程过程调用(RPC)在诸如 DCOM 与 CORBA 等对象之间进行通信,但是 H ...
- ListView列表拖拽排序
ListView列表拖拽排序能够參考Android源代码下的Music播放列表,他是能够拖拽的,源代码在[packages/apps/Music下的TouchInterceptor.java下]. 首 ...
- xml--通过DOM解析XML
此文章通过3个例子表示DOM方式解析XML的用法. 通过DOM解析XML必须要写的3行代码. step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) step 2:获得具体的dom解 ...
- rk3288 ov8858 camera移植
平台:瑞芯的rk3288 SDK:4.4/5.0/5.1 作者:fulinux *****本文同意转载.只是请注明出处:http://blog.csdn.net/fulinus**** rk3288的 ...
- Java基础知识强化83:System类之gc()方法(垃圾回收)以及和finalize()区别
1. System概述: System类包含一些有用的类字段和方法.它不能被实例化. 2. gc()方法:垃圾回收器 public static void gc() 调用gc方法暗示着Ja ...
- linux sudo环境变量设置
这是第一次在博客园中写自己的随笔,也是第一次使用Markdown的编辑环境,值得纪念一下!希望在以后学习linux的道路上能多多记录自己的学习心得和遇到问题的解决方法,朝着运维的方向迈出坚实的脚步. ...
- java的练习
import java.awt.GridLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.s ...
- innodb的innodb_buffer_pool_size和MyISAM的key_buffer_size
一. key_buffer_size 对MyISAM表来说非常重要. 如果只是使用MyISAM表,可以把它设置为可用内存的 30-40%.合理的值取决于索引大小.数据量以及负载 -- 记住,MyISA ...