【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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 单片机好学还是plc好学_单片机出路

    单片机好学还是plc好学_单片机出路相信很多学电气工程专业的都会学习PLC,我当初也是电气工程专业,主要学的三菱PLC,后面也玩了下西门子的。当时觉得还挺神奇,也对编程比较感兴趣,不过学校学得太简单了,基本让你编个梯形图控制电机就算是毕业了。后来我就转去做单片机开发了,感觉比PLC更好玩,因为成本低,灵活性也高,可玩性自然也更高。最近我们无际单片机编程也有几个学员是做PLC转行过来学单片机的。我没从事过PLC的工作,根据他们描述,PLC的工资其实也还行,基本也能过万,但是就是出差太频繁,一年300天在外面出差。如果是单身寡

    2022年8月31日
    0
  • mysql数据库报错1146_数据库错误代码1146 – 本地与在线

    mysql数据库报错1146_数据库错误代码1146 – 本地与在线我是这个站点的新手-请温和请:Plocalhost上的mySQL数据库适用于插入语句,但只要将数据库连接更改为服务器连接它给了我错误:错误代码1146:1146没有任何错误描述。可能是什么原因?根据我的错误捕获逻辑,连接一直成功到查询运行的一部分。在本地版本上,它就像一个魅力。有任何想法吗?数据库错误代码1146-本地与在线:::::::::::::::::::::::::::::::::…

    2022年6月1日
    67
  • 二叉搜索树,超强实用讲解

    二叉搜索树,超强实用讲解

    2021年9月28日
    40
  • 图像语义分割简介_图像语义分割算法

    图像语义分割简介_图像语义分割算法今天,我们就来谈谈自动驾驶系统中的一项重要核心技术——图像语义分割(Semanticimagesegmentation)。图像语义分割作为计算机视觉(Computervision)中图像理解(Imageunderstanding)的重要一环,不仅在工业界的需求日益凸显,同时语义分割也是当下学术界的研究热点之一。什么是图像语义分割?图像语义分割可以说是

    2022年8月21日
    4
  • 纯CSS实现自定义单选框和复选框

    纯CSS实现自定义单选框和复选框<!DOCTYPEhtml><html> <head> <metacharset=”utf-8″> <title></title> <styletype=”text/css”> #main{ display:flex; justify-content:center; align-items:center; flex-wrap:wrap; } .

    2022年5月29日
    31
  • 常见的几种矩阵分解方式

    常见的几种矩阵分解方式1.三角分解(LU分解)矩阵的LU分解是将一个矩阵分解为一个下三角矩阵与上三角矩阵的乘积。本质上,LU分解是高斯消元的一种表达方式。首先,对矩阵A通过初等行变换将其变为一个上三角矩阵。对于学习过线性代数的同学来说,这个过程应该很熟悉,线性代数考试中求行列式求逆一般都是通过这种方式来求解。然后,将原始矩阵A变为上三角矩阵的过程,对应的变换矩阵为一个下三角矩阵。这中间的过程,就是Doolittleal

    2022年5月30日
    39

发表回复

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

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