Popwindow的介绍和使用


前言

本篇文章主要介绍Popwindow的基本使用方法,首先根据官方文档,介绍它的构造方法,属性,和部分方法介绍,细节请查看官方文档,然后介绍我在项目中是怎么使用的,以及遇到的问题。本篇文章适用新手,高手绕行。下面进入正题:

官方介绍

1.概述

Popwindow可以包含任意视图,是浮动在Activity之上显示的。

2.构造方法

它有9个构造方法,可以通过 AttributeSet,样式去构造,一般使用View,height,width构造就可以了。
PopupWindow ( View  contentView, int width, int height),PopupWindow(View contentView, int width, int height, boolean focusable) 
focusable设置是否获取焦点。

3.常用方法介绍

文档中的公有方法包含以下几类方法,get方法获取控件的特有属性,set方法设置控件的属性,is判读控件的状态,show设置控件的位置以及展示,update方法更新控件位置和宽高,dismiss隐藏控件。
public void showAtLocation (View parent, int gravity, int x, int y)
显示在布局中的任意位置,通过gravity,x,y指定;
public void showAsDropDown (View anchor, int xoff, int yoff)
显示在一个锚点之下,有时候布局需要调整显示的位置,可以通过xoff,yoff偏移量调整显示;
public void update (View anchor, int xoff, int yoff, int width, int height)
当PopupWindow显示时,可以通过update方法修改界面显示位置和宽高,例如有些界面点击不同的部分会弹出不同位置的提示就可以通过这种方式实现。
public void setAnimationStyle (int animationStyle)
这个可以为PopupWindow设置显示和隐藏动画,具体的动画怎么设置下面将会详细讲解步骤。
这几个是一般常用的方法,当然,使用一个控件,一定需要对这个控件能够熟练的掌握,在这儿只是抛砖引玉,需要同学你自己对官方文档大概还是看看,因为还有比如设置控件外是否可点隐藏,背景设置这些不可能都详细讲解。

4.项目中使用

使用场景:公司一个项目需要在底部显示一个弹出框,背景需要变暗,点击取消按钮才隐藏底部框。因为背景慢慢变暗是一个动画,所以我在主布局中添加了一个覆盖层来控制效果的实现,以下是我写得一个Demo。

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

    <TextView
        android:id="@+id/popwindow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_centerInParent="true"
        android:background="#ff0000"
        android:text="弹出提示框" />
    <TextView
        android:id="@+id/updatepopwindow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_centerInParent="true"
        android:background="#ff0000"
        android:layout_below="@id/popwindow"
        android:layout_marginTop="10dp"
        android:text="菜单式提示" />
    <FrameLayout 
        android:id="@+id/container_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#aaaaaa"
        ></FrameLayout>
</RelativeLayout>
这是主布局文件。

package com.example.popupwindowdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	FrameLayout mAnchorView;
	
	PopupWindow mPopupWindow;
	View popView;
	TextView text1;
	TextView text2;
	
	TextView tvToPop;
	TextView updatepopwindow;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mAnchorView = (FrameLayout) findViewById(R.id.container_main);
		mAnchorView.setVisibility(View.GONE);
		tvToPop = (TextView) findViewById(R.id.popwindow);
		updatepopwindow = (TextView) findViewById(R.id.updatepopwindow);
		tvToPop.setOnClickListener(this);
		updatepopwindow.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.popwindow:
//			showPopupWindow();
			showPopupWindowToBtn();
			Toast.makeText(this, "showPopupWindow", 1).show();
			break;
		case R.id.text_first:
			Toast.makeText(this, "text_first", 1).show();
			break;
		case R.id.text_second:
			Toast.makeText(this, "text_second", 1).show();
			updatePop();
			break;
		case R.id.updatepopwindow:
			Toast.makeText(this, "updatepopwindow", 1).show();
			break;

		default:
			break;
		}
	}
	
	private void updatePop(){
		mPopupWindow.update(updatepopwindow, popView.getWidth(), popView.getHeight());
	}
	
	private void showPopupWindowBottom(){
		if(mPopupWindow == null){
			setPopView();
			setPopupWindow();
		}
		if(!mPopupWindow.isShowing()){
			setAnimEnter(mAnchorView);
			mAnchorView.setVisibility(View.VISIBLE);
			mAnchorView.setClickable(true);
			mPopupWindow.showAtLocation(mAnchorView, Gravity.BOTTOM, 0, 0);
		}
	}
	
	private void showPopupWindowToBtn(){
		if(mPopupWindow == null){
			setPopView();
			setPopupWindow();
		}
		if(!mPopupWindow.isShowing()){
			setAnimEnter(mAnchorView);
			mAnchorView.setVisibility(View.VISIBLE);
			mAnchorView.setClickable(true);
			mPopupWindow.showAsDropDown(tvToPop);
		}
	}
	
	private void hidePopupWindow(){
		if(mPopupWindow != null && mPopupWindow.isShowing()){
			setAnimExit(mAnchorView);
			mPopupWindow.dismiss();
			mAnchorView.setVisibility(View.GONE);
			mAnchorView.setClickable(false);
		}
	}
	
	private void setPopView(){
		popView = LayoutInflater.from(this).inflate(R.layout.ac_pop,null);
		text1 = (TextView) popView.findViewById(R.id.text_first);
		text2 = (TextView) popView.findViewById(R.id.text_second);
		text1.setOnClickListener(this);
		text2.setOnClickListener(this);
	}
	
	private void setPopupWindow(){
		mPopupWindow = new PopupWindow(popView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//		mPopupWindow.setAnimationStyle(R.style.PopWindowAnimStyle);
	}
	
	private void setAnimEnter(View view){
		AlphaAnimation anim = new AlphaAnimation(0.0f, 0.5f);
		anim.setDuration(500);
		anim.setFillAfter(true);
		view.startAnimation(anim);
	}
	
	private void setAnimExit(View view){
		AlphaAnimation anim = new AlphaAnimation(0.5f, 0.0f);
		anim.setDuration(500);
		anim.setFillAfter(true);
		view.startAnimation(anim);
	}

}
这是主Activity,用来控制各种展示效果。

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="100%p"
    android:toYDelta="0"
    android:duration="500"
    android:fillEnabled="true"
    android:fillAfter="true"
    >
</translate>
这是pop_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="100%p"
    android:duration="500"
    android:fillEnabled="true"
    android:fillAfter="true"
    >
</translate>
这是pop_exit.xml
<style name="PopWindowAnimStyle" parent="android:Animation" mce_bogus="1">
        <item name="android:windowEnterAnimation">@anim/pop_enter</item>
        <item name="android:windowExitAnimation">@anim/pop_exit</item>
    </style>
在styles文件中定义一个style,在开篇提到过使用
setAnimationStyle (int animationStyle)
来设置动画。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值