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. ffmpeg-20160811-bin.7z

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  2. 【编程题目】在从 1 到 n 的正数中 1 出现的次数

    30.在从 1 到 n 的正数中 1 出现的次数(数组)题目:输入一个整数 n,求从 1 到 n 这 n 个整数的十进制表示中 1 出现的次数.例如输入 12,从 1 到 12 这些整数中包含 1 的 ...

  3. Oracle数据库对象题库

    一.    填空题 在用 create 语句创建基本表时,最初只是一个空的框架,用户可以使用insert命令把数据插入表中. 在基本表不需要时,可以使用 drop table 语句撤消.在一个基本表撤 ...

  4. 模拟操作网页 webBrowser

    C# 获取IFrame中body元素 (winform) 方法1. 找出iframe的b.html的src , 利用webbrowser去加载b.html HtmlElementCollection ...

  5. [Android Pro] 网络流量安全测试工具Nogotofail

    reference to : http://www.freebuf.com/tools/50324.html 从严重的HeartBleed漏洞到苹果的gotofail 漏洞,再到最近的SSL v3 P ...

  6. [Android Pro] InputStream.skip方法的思考

    参考 : http://blog.csdn.net/gsyzhu/article/details/8102286 在java.io.InputStream类中定义了skip这个方法.在API中的描述如 ...

  7. 容器配置jndi Tomcat为例

    context.xml 文件 <Resource name=" password="spsj" type="javax.sql.DataSource&qu ...

  8. 转载_虚拟机下LInux(终端)配置网络的方法

    出自: http://www.360doc.com/content/14/1027/11/17496895_420258403.shtml 对文章的重点进行剪贴,方便查看. 这几天在虚拟机vmware ...

  9. 从xml中构建sqlSessionFactory

    String resource = "org/mybatis/example/Configuration.xml"; Reader reader = Resources.getRe ...

  10. EventBus学习入门

    EventBus Features What makes greenrobot's EventBus unique, are its features: Simple yet powerful: Ev ...