【Android开发日记】Popupwindow 完美demo
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的更多相关文章
- 【Android开发日记】之入门篇(八)——Android数据存储(下)
废话不多说了,紧接着来讲数据库的操作吧.Come On! 提到数据存储问题,数据库是不得不提的.数据库是用来存储关系型数据的不二利器.Android为开发者提供了强大的数据库支持,可以用来轻松地构造基 ...
- 【Android开发日记】之入门篇(九)——Android四大组件之ContentProvider
数据源组件ContentProvider与其他组件不同,数据源组件并不包括特定的功能逻辑.它只是负责为应用提供数据访问的接口.Android内置的许多数据都是使用ContentProvider形式,供 ...
- 【Android开发日记】之入门篇(五)——Android四大组件之Service
这几天忙着驾校考试,连电脑都碰不到了,今天总算告一段落了~~Service作为Android的服务组件,默默地在后台为整个程序服务,辅助应用与系统中的其他组件或系统服务进行沟通.它跟Activity的 ...
- 【Android开发日记】之入门篇(六)——Android四大组件之Broadcast Receiver
广播接受者是作为系统的监听者存在着的,它可以监听系统或系统中其他应用发生的事件来做出响应.如设备开机时,应用要检查数据的变化状况,此时就可以通过广播来把消息通知给用户.又如网络状态改变时,电量变化时都 ...
- 【Android开发日记】之入门篇(四)——Android四大组件之Activity
在Android中,无论是开发者还是用户,接触最多的就算是Activity.它是Android中最复杂.最核心的组件.Activity组件是负责与用户进行交互的组件,它的设计理念在很多方面都和Web页 ...
- 【Android开发日记】之入门篇(十一)——Android的Intent机制
继续我们的Android之路吧.今天我要介绍的是Android的Intent. 对于基于组件的应用开发而言,不仅需要构造和寻找符合需求的组件,更重要的是要将组件有机的连接起来,互联互通交换信息,才能够 ...
- 【Android开发日记】之入门篇(七)——Android数据存储(上)
在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也 ...
- 【Android开发日记】之入门篇(十二)——Android组件间的数据传输
组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...
- 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件
好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...
随机推荐
- java web 学习 --第七天(Java三级考试)
第六天的学习内容如下:http://www.cnblogs.com/tobecrazy/p/3462244.html application application对象的方法与应用: ① setA ...
- 手记-数学分析(高等数学)中有关算法效率的公式列举(O,Θ,Ω)
权当数据结构与算法分析的学习手记 系数为一的幂级数部分和公式 ∑ n2 = 12 + 22 + 32 + ... + n2 = n(n+1)(2n+1)/6 = O(n3) ∑ n3 = ...
- Match:Blue Jeans(POJ 3080)
DNA序列 题目大意:给你m串字符串,要你找最长的相同的连续字串 这题暴力kmp即可,注意要按字典序排序,同时,是len<3才输出no significant commonalities #in ...
- Solr安装过程
Solr安装过程 下载相关资料 solr 4.2.0 http://lucene.apache.org/solr/ 期间安装过 solr 4.3.0 很可惜没有配置成功 apache-tomcat-7 ...
- suse linux 命令
1.修改vftpd配置文件 vi /etc/vsftpd .conf #listen=YES vi /etc/xinetd.d/vsftpd ...
- oracle触发器,一个表新增、修改的同时同步另一张表
oracle创建触发器,把本地新增.修改数据过程同步到另一个服务器上去. 如果是本地,加数据库名即可.如果是远程服务器,不是一台机器,做一个db_link操作即可. ----------------- ...
- SharedPreferences详解(三)——存取图片
MainActivity如下: package cc.sp; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputSt ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- PHP安全编程:不要让不相关的人看到报错信息
没有不会犯错的开发者,PHP的错误报告功 能可以协助你确认和定位这些错误,可以提供的这些错误的详细描述,但如果被恶意攻击者看到,这就不妙了.不能让大众看到报错信息,这一点很重要.做到这一 点很容易,只 ...
- September 21st 2016 Week 39th Wednesday
Don't try so hard, the best things come when you least expect them. 不要着急,最好的总会在最不经意的时候出现. Always tur ...