简单 android popupwindow 实现
1、 popupWindow 设置大小;
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
2、showAtLocation 方法是将popupWindow显示位置 ;
popupWindow.showAtLocation(v, Gravity.CENTER_HORIZONTAL,
0,
0);
3、PopUpWindow dimiss隐藏,需要 PopupWindow 显示之前设置它的背景不为空:如下面两行代码:
ColorDrawable cd = new ColorDrawable(-0000);
popupWindow.setBackgroundDrawable(cd);
注意这里设置背景并不会覆盖xml文件定义的背景。
4、 当有popupWindow.setFocusable(false);的时候,说明PopupWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失;
但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popupWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。
5、 // 这里设置显示PopupWindow之后在外面点击是否有效。如果为false的话,那么点击PopupWindow外面并不会关闭PopupWindow。当然这里很明显只能在Touchable下才能使用。
popupWindow.setOutsideTouchable(true);
下面给出一个简单的使用PopuWindow的实例:
package com.popupwindow;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.SpannableString;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends Activity
{
PopupWindow popupWindow;
Button imgCall;
private View view;
private Context mContext;
MarginLayoutParams margin;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
imgCall = (Button) findViewById(R.id.showbutton);
imgCall.setOnClickListener(new PopupOnClickListener());
margin=new MarginLayoutParams(imgCall.getLayoutParams());
}
/**
* 这个类主要显示PopuWindow,并显示之后对里面的按钮添加监听事件。
*/
private class PopupOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
System.out.println("onClick myCursor");
switch (v.getId()) {
case R.id.showbutton:
initPopupWindow();
// 加上下面两行可以用back键关闭popupwindow,否则必须调用dismiss();
// 需要顺利让PopUpWindow dimiss;PopUpWindow的背景不能为空。
// 当有popuWindow.setFocusable(false);的时候,说明PopuWindow不能获得焦点,并不能点击外面消失,只能由dismiss()消失。
// 当设置为popuWindow.setFocusable(true);的时候,加上下面两行代码才会消失
// 注意这里添加背景并不会覆盖原来的背景。
ColorDrawable cd = new ColorDrawable(-0000);
popupWindow.setBackgroundDrawable(cd);
popupWindow.showAtLocation(v, Gravity.CENTER_HORIZONTAL,
0,
0);
break;
default:
break;
}
}
}
private void initPopupWindow() {
view = getInfoWindow();//this.getLayoutInflater().inflate(R.layout.custom_info_window, null);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// 这里设置显示PopupWindow之后在外面点击是否有效。如果为false的话,那么点击PopupWindow外面并不会关闭PopuWindow。
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);//不能在没有焦点的时候使用
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (popupWindow.isShowing()) {
popupWindow.dismiss();
}
}
public View getInfoWindow()
{
View infoWindow = ((Activity) mContext).getLayoutInflater().inflate(
R.layout.custom_info_window, null);
render(infoWindow);
return infoWindow;
}
public void render( View view)
{
String title = "我的弹出框";
TextView titleUi = ((TextView) view.findViewById(R.id.title));
if(title != null)
{
SpannableString titleText = new SpannableString(title);
titleUi.setText(titleText);
}
String snippet = "姓名:李VV\n性别:男\n出生日期:1990/12/12 12:10:05\n所在地:北京";
TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
if(snippet != null)
{
SpannableString snippetText = new SpannableString(snippet);
snippetUi.setText(snippetText);
}
}
}
具体下载路径:http://download.csdn.net/detail/q610098308/8211811
简单 android popupwindow 实现的更多相关文章
- Android PopupWindow的使用技巧(转)
Android PopupWindow的使用技巧 PopupWindow是Android上自定义弹出窗口,使用起来很方便. PopupWindow的构造函数为 public PopupWindow(V ...
- Android PopupWindow Dialog 关于 is your activity running 崩溃详解
Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...
- Android PopupWindow的使用和分析
Android PopupWindow的使用和分析 PopupWindow使用 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activi ...
- Android—PopupWindow的简单使用
PopupWindow 是一个可以显示在当前 Activity 之上的浮动容器,这个Demo要实现的功能是,点击布局中的两个按钮,进而控制PopupWindow的显示与消失,代码中有详细的注释首先看一 ...
- Android 属性动画实现一个简单的PopupWindow
1.今天看到一个PopupWindow的效果如图: 2.其实就是属性动画的一个简单实用就ObjectAnimator就可以的,想实现更多,更灵活的可以用ValueAnimator 3.直接上代码: p ...
- 不得不吐槽的Android PopupWindow的几个痛点(实现带箭头的上下文菜单遇到的坑)
说到PopupWindow,我个人感觉是又爱又恨,没有深入使用之前总觉得这个东西应该很简单,很好用,但是真正使用PopupWindow实现一些效果的时候总会遇到一些问题,但是即便是人家的api有问题, ...
- Android PopupWindow 仿微信弹出效果
项目中,我须要PopupWindow的时候特别多,这个东西也特别的好使,所以我今天给大家写一款PopupWindow 仿微信弹出效果.这样大家直接拿到项目里就能够用了! 首先让我们先看效果: 那么我首 ...
- Android PopupWindow使用方法小结
前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法. 使用场景 PopupWindow,顾名思义,就是弹窗,在很多 ...
- Android popupwindow使用心得(一)
最近项目中好多地方用到popupwindow,感觉这个控件还是非常重要的.所以把使用心得总结下,废话不多说,直接上代码. public class MainActivity extends Activ ...
随机推荐
- HDU - 6446 Tree and Permutation
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6446 本题是一个树上的问题——DFS. 一棵N个结点的树,其结点为1~N.树具有N-1条边,每一条边具有 ...
- 【Codeforces 300C】Beautiful Numbers
[链接] 我是链接,点我呀:) [题意] 让你找到长度为n的数字 这个数字只由a或者b组成 且这n个数码的和也是由a或者b组成的 求出满足这样要求的数字的个数 [题解] 枚举答案数字中b的个数为y,那 ...
- tmux使用入门
tmux是Linux中窗口管理程序,适用于终端复用,尤其适合远程连接.最近,我正苦闷与ssh自动超时退出和broken pipe,决定投入tmux怀抱. 使用tmux最直接的好处,便是可以在一个远程连 ...
- O - Treats for the Cows 区间DP
FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast am ...
- Eclipse代码/目录虚线对齐设置
前提: 我的Eclipse版本如下: 比这个版本新或者旧都可以实现如下效果. 实现步骤: 在代码上显示虚线设置有如下方法: 1.如果不使用插件,Eclipse是不支持虚线的,只能是横条的点状,效果如下 ...
- 条款39: 避免 "向下转换" 继承层次
基类指针不能调用派生类的独有的成员,即使基类指针指向派生类对象,因为编译器是根据指针的静态类型来确定调用对象在内存中占据的空间的.此时可以使用static_cast来转换,但不要这么做,因为向下转换难 ...
- 在InternetExplorer.Application中显示本地图片
忘记了,喜欢一个人的感觉 Demon's Blog » 程序设计 » 在InternetExplorer.Application中显示本地图片 « 对VBS效率的再思考——处理二进制数据 Wo ...
- automaticallyAdjustsScrollViewInsets 使用
automaticallyAdjustsScrollViewInsets(个人认为iOS7中略坑爹的属性) @当我们在一个UIViewController中同时创建2个tableView的时候,如果把 ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- HBase无法连接ZooKeeper问题
上次搭建HBase环境后,运行登陆server时,报以下的错误: hadoop@gpmaster logs]$ hbase shell SLF4J: Class path contains multi ...