【Android开发日记】Popupwindow 完美demo

【Android开发日记】Popupwindow 完美demo

大家好,又见面了,我是全栈君。

Popupwindow 完美demo实现

图示:

                                                                                                      【Android开发日记】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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116508.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何写 Cover Letter 论文投稿「建议收藏」

    如何写 Cover Letter 论文投稿「建议收藏」论文投稿时一般要求附带一篇“CoverLetter”,好的“CoverLetter”不仅可以使文章锦上添花,更有可能直接决定论文是否被录用。本人切身体会,以已发表论文为例,现将其写法经验归纳如下:Dear Editor,We would like to submit the enclosed manuscript entitled "论文题目", which we wish to be con…

    2022年6月4日
    34
  • com.jcraft.jsch.JSchException: Auth fail

    背景服务器信息: 服务器A:10.102.110.1 服务器B:10.102.110.2 需要从服务器A通过Sftp传输文件到服务器B。应用项目中有一个功能,要通个关Sftp进行日志文件的传输,在部署的时候,服务器之间已经配置了免认证(密),也就sftp免密登录,但是部署完项目后,启动服务,在需要传输的时候还是报了下面的错误: com.jcraft.jsch.JSchExcep…

    2022年2月27日
    340
  • 【愚公系列】2022年02月 wireshark系列-数据抓包分析之DHCP协议

    【愚公系列】2022年02月 wireshark系列-数据抓包分析之DHCP协议实验步骤一获取DHCP数据包在windows平台上获取DHCP数据包在windows平台上,可以使用两种简单的方法实现,其原理一样。(1)在cmd上,使用ipconfig命令来获取。执行完上述命令后,将释放当前使用的地址信息。重新获取地址信息,执行命令如下执行完上面的命令后,将重新获取地址信息。在获取地址时,将会经过上面讲述的DHCP的4个阶段。这样,我们就能获取到DHCP数据包了。(2)通过禁用和启用网卡获取DHCP数据包在windows平台上,也可以通过禁用和启用网卡获取DHCP数

    2022年5月23日
    45
  • 阿里java架构成长笔记_职场知识点分享

    阿里java架构成长笔记_职场知识点分享1.源码分析专题详细介绍源码中所用到的经典设计思想,看看大牛是如何写代码的,提升技术审美、提高核心竞争力。帮助大家寻找分析源码的切入点,在思想上来一次巨大的升华。知其然,并知其所以然。把知识变成自己的2.分布式架构互联网时代,系统架构如何迎接高并发流量的挑战。而作为技术开发者,如何去应对技术变革带来的技能危机。基于传统架构到分布式架构演变过程所带来的技术变革进行全面深入讲解…

    2025年11月10日
    11
  • 分水岭算法 matlab实现

    分水岭算法 matlab实现背景     做图像分割的时候用到了,就学习了一下大概思想     把图像中的像素大小理解成山地的海拔,向山地灌水,海拔低的地方会积水,这些地方称之为谷底。随着水位上升,不同谷底的水会相遇,相遇的地方就是分水岭。    &nbs

    2022年6月17日
    26
  • HDLBits答案(12)_Verilog移位寄存器「建议收藏」

    HDLBits答案(12)_Verilog移位寄存器「建议收藏」Verilog移位寄存器HDLBits链接前言今天更新一节寄存器相关内容,其中涉及CRC校验的内容是用线性反馈移位寄存器搭建而成的。题库题目描述1:构建一个4bit的移位寄存器(右移),含异步复位、同步加载和使能areset:让寄存器复位为0load:加载4bit数据到移位寄存器中,不移位ena:使能右移q:移位寄存器中的内容Solution1:moduletop_module(inputclk,inputareset,//asyncacti

    2022年7月16日
    16

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号