Popupwindow 完美demo实现

图示:

关键代码说明:

1.弹出popupwindow,背景变暗

ColorDrawable cd = new ColorDrawable(0x000000);
popuWindow1.setBackgroundDrawable(cd);
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha = 0.4f;
getWindow().setAttributes(lp);

2.popupwindow消失之后,背景恢复

public void onDismiss(){
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}

3.popupwindow圆角矩形

rounded_corners_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"/>
</shape>

pupopwindow布局文件中设置背景

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/rounded_corners_bg"
android:orientation="vertical" >
...
...
...
</RelativeLayout >

4.点击popupwindow外部,popupwindow也会dismiss

popuWindow1.setOutsideTouchable(true);
popuWindow1.setFocusable(true);

完整代码:

1.MainActivity.java

package com.popupwindowdemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.PopupWindow.OnDismissListener; public class MainActivity extends Activity { //popupwindow
private PopupWindow popuWindow1;
private View contentView1;
private TextView textview1;
private Button btn1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textview1 = (TextView)findViewById(R.id.textView2);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
initPopuWindow1(v);
}
}); textview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/geeklei/article/"));
startActivity(intent);
}
});
} private void initPopuWindow1(View parent) {
if (popuWindow1 == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(this);
contentView1 = mLayoutInflater.inflate(R.layout.popuwindow1, null);
popuWindow1 = new PopupWindow(contentView1,ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
} ColorDrawable cd = new ColorDrawable(0x000000);
popuWindow1.setBackgroundDrawable(cd);
//产生背景变暗效果
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha = 0.4f;
getWindow().setAttributes(lp); popuWindow1.setOutsideTouchable(true);
popuWindow1.setFocusable(true);
popuWindow1.showAtLocation((View)parent.getParent(), Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0); popuWindow1.update();
popuWindow1.setOnDismissListener(new OnDismissListener(){ //在dismiss中恢复透明度
public void onDismiss(){
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
} }

2.activity_main.xml

<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.popupwindowdemo.MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="23dp"
android:text="click to visit my csdn blog"
android:textColor="#1C86EE" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="103dp"
android:text="click to show popupwindow" /> </RelativeLayout>

3.popupwindow1.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/rounded_corners_bg" android:orientation="vertical" > <LinearLayout
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical|center"
android:orientation="vertical" > <TextView
android:id="@+id/group_menu1_passfile"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="item1"
android:textColor="#000000"
android:textSize="13sp" /> <View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#999999" />
<TextView
android:id="@+id/group_menu1_playgame"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="item2"
android:textColor="#000000"
android:textSize="13sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#999999" /> <TextView
android:id="@+id/group_menu1_removefriend"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="item3"
android:textColor="#000000"
android:textSize="13sp" /> <View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#999999" /> <TextView
android:id="@+id/group_menu1_setremark"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="item4"
android:textColor="#000000"
android:textSize="13sp" /> <View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#999999" /> <TextView
android:id="@+id/group_menu1_brokemate"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="item5"
android:textColor="#000000"
android:textSize="13sp" /> </LinearLayout>
</RelativeLayout>

4.rounded_corners_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"/>
</shape>

demo下载地址:

http://download.csdn.net/detail/geeklei/7991521

【Android开发日记】Popupwindow 完美demo的更多相关文章

  1. 【Android开发日记】之入门篇(八)——Android数据存储(下)

    废话不多说了,紧接着来讲数据库的操作吧.Come On! 提到数据存储问题,数据库是不得不提的.数据库是用来存储关系型数据的不二利器.Android为开发者提供了强大的数据库支持,可以用来轻松地构造基 ...

  2. 【Android开发日记】之入门篇(九)——Android四大组件之ContentProvider

    数据源组件ContentProvider与其他组件不同,数据源组件并不包括特定的功能逻辑.它只是负责为应用提供数据访问的接口.Android内置的许多数据都是使用ContentProvider形式,供 ...

  3. 【Android开发日记】之入门篇(五)——Android四大组件之Service

    这几天忙着驾校考试,连电脑都碰不到了,今天总算告一段落了~~Service作为Android的服务组件,默默地在后台为整个程序服务,辅助应用与系统中的其他组件或系统服务进行沟通.它跟Activity的 ...

  4. 【Android开发日记】之入门篇(六)——Android四大组件之Broadcast Receiver

    广播接受者是作为系统的监听者存在着的,它可以监听系统或系统中其他应用发生的事件来做出响应.如设备开机时,应用要检查数据的变化状况,此时就可以通过广播来把消息通知给用户.又如网络状态改变时,电量变化时都 ...

  5. 【Android开发日记】之入门篇(四)——Android四大组件之Activity

    在Android中,无论是开发者还是用户,接触最多的就算是Activity.它是Android中最复杂.最核心的组件.Activity组件是负责与用户进行交互的组件,它的设计理念在很多方面都和Web页 ...

  6. 【Android开发日记】之入门篇(十一)——Android的Intent机制

    继续我们的Android之路吧.今天我要介绍的是Android的Intent. 对于基于组件的应用开发而言,不仅需要构造和寻找符合需求的组件,更重要的是要将组件有机的连接起来,互联互通交换信息,才能够 ...

  7. 【Android开发日记】之入门篇(七)——Android数据存储(上)

    在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也 ...

  8. 【Android开发日记】之入门篇(十二)——Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...

  9. 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件

        好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...

随机推荐

  1. java web 学习 --第九天(Java三级考试)

    第八天的学习内容如下:http://www.cnblogs.com/tobecrazy/p/3468458.html Java servlet 技术 Servlet是使用java servlet应用程 ...

  2. 警告: 程序集绑定日志记录被关闭(IIS7 64位系统)

    部署一个.NET程序在IIS7服务器,出现如下信息: 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: S ...

  3. [Android] how to get facebook profile

    Bundle params = new Bundle(); params.putString("fields", "id,email,gender,cover,pictu ...

  4. Effective C++ -----条款19:设计class犹如设计type

    Class的设计就是type的设计.在定义一个新type之前,请确定你已经考虑过本条款覆盖的所有讨论主题. 新type的对象应该如何被创建和销毁? 对象的初始化和对象的赋值该有什么样的区别? 新typ ...

  5. .Net如何在后台设置日期格式,并显示在前台页面上

    其实方法比较老咯,有比这个简单的朋友请留言哈,我的思路是先将数据库中的日期格式读出来,在后台转化成DatetTime类型,然后在使用DateTime的内部方法设置日期格式,代码如下: DateTime ...

  6. [Android Pro] 小心ReleaseByteArrayElements 中的参数问题

    referen to : http://blog.csdn.net/rainlight/article/details/818964 在Sun的官方文档中,关于该函数的用法如下 The array i ...

  7. iOS高效调试

    写代码难免出现bug. 储备些调试技能绝对能够提高你的工作效率,让bug无所遁形.下面就和大家分享一些我在工作中常用的iOS调试小技能. 1. 打印 最简单,基础的调试方法就是打印日志了.贴出两段封装 ...

  8. web storm

    常用插件: code glance 代码预览功能,用过的都知道有多爽...

  9. JDK、Jmeter、Android环境变量配置

    JDK环境变量 1.在系统变量里点击新建,变量名填写JAVA_HOME,变量值填写JDK的安装路径,在这里就填写"D:\Program Files\Java\jdk1.6.0_26" ...

  10. 三、jQuery--jQuery插件--jQuery插件——Validation Plugin

    简介: 客户端验证:现代网站填写表单时,几乎一定会采用的方式. 优点:1.可以减少服务器压力 2.缩短用户等待时间和提升用户体验 jQuery有很多表单验证插件:https://plugins.jqu ...