当前位置: 首页 > news >正文

【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"    

相关文章:

  • Git代码仓库的建立流程
  • 从0开始学习 Git
  • Oracle 12c Study之--Installer Oracle
  • 事务隔离级别小记
  • C# 语言规范_版本5.0 (第1章 介绍)
  • Oracle创建存储过程、执行存储过程基本语法
  • java- Java IO
  • qt 共享内存 单例
  • phpcms V9 内容模型管理(转)
  • Swift开发小技巧--自定义Log
  • 练习:WinForm(控件Button,打开,关闭窗体)
  • html中,文件上传时使用的input type=file的样式自定义
  • 用DBHelper在JSP页面中实现登录功能
  • Haproxy------在windows下配置负载均衡
  • redis使用及配置之缓存详解
  • [nginx文档翻译系列] 控制nginx
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • Java 网络编程(2):UDP 的使用
  • JDK 6和JDK 7中的substring()方法
  • js正则,这点儿就够用了
  • laravel with 查询列表限制条数
  • Laravel 中的一个后期静态绑定
  • log4j2输出到kafka
  • Mocha测试初探
  • mockjs让前端开发独立于后端
  • Promise面试题2实现异步串行执行
  • React Native移动开发实战-3-实现页面间的数据传递
  • Vue2.0 实现互斥
  • 爱情 北京女病人
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 前端面试之CSS3新特性
  • 数据可视化之 Sankey 桑基图的实现
  • const的用法,特别是用在函数前面与后面的区别
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • 数据库巡检项
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (原創) 系統分析和系統設計有什麼差別? (OO)
  • (转)【Hibernate总结系列】使用举例
  • (转)memcache、redis缓存
  • (转)四层和七层负载均衡的区别
  • (转)一些感悟
  • .NET Core中的去虚
  • .net FrameWork简介,数组,枚举
  • .NET 跨平台图形库 SkiaSharp 基础应用
  • .net 受管制代码
  • .vue文件怎么使用_vue调试工具vue-devtools的安装
  • // an array of int
  • ?php echo ?,?php echo Hello world!;?
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解