您的位置:首页 > 运维架构

PopupWindow泡泡窗口

2016-04-04 18:08 351 查看
pop的布局文件的根部元素如果设置为 android:layout_height="match_parent"

并且代码中设置了高度为: LayoutParams.WRAP_CONTENT

PopupWindow mPopWindow = new PopupWindow(popview,  
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);  

显示的时候pop的根元素的android:layout_height="match_parent"会失效,

实验二如果pop的布局文件的根部元素如果设置为 android:layout_height="wrap_content"

代码中设置了高度为: LayoutParams.MATCH_PARENT

显示效果是按照.MATCH_PARENT显示的

实验三同时设置wep_content 显示效果为wep_conten

我发现:pop的布局文件的根部元素宽高设置在显示的时候不会有效果,原因我猜测是被

代码中设置的效果覆盖了

PopupWindow mPopWindow = new PopupWindow(popview,  
 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);  


使用泡泡窗口经常用到的

//pop窗口弹出后修改窗口透明值使窗口背景变暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.5f; //0.0-1.0
getWindow().setAttributes(lp);




mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//popupwindow消失的时候恢复成原来的透明度
lp.alpha = 1.0f;
getWindow().setAttributes(lp);
}
});




参考案例:

http://www.tuicool.com/articles/NZjMfyA

案例:


【Android】创建Popwindow弹出菜单的两种方式


方法一的Activity

package com.app.test02;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class PopwindowLeft extends Activity {
// 声明PopupWindow对象的引用
private PopupWindow popupWindow;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popupwindow_main);
// 点击按钮弹出菜单
Button pop = (Button) findViewById(R.id.popBtn);
pop.setOnClickListener(popClick);
}

// 点击弹出左侧菜单的显示方式
OnClickListener popClick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getPopupWindow();
// 这里是位置显示方式,在屏幕的左侧
popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
}
};

/**
* 创建PopupWindow
*/
protected void initPopuptWindow() {
// TODO Auto-generated method stub
// 获取自定义布局文件activity_popupwindow_left.xml的视图
View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,
false);
// 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);
// 设置动画效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
// 点击其他地方消失
popupWindow_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
}
/***
* 获取PopupWindow实例
*/
private void getPopupWindow() {
if (null != popupWindow) {
popupWindow.dismiss();
return;
} else {
initPopuptWindow();
}
}
}



方法二的Activity

package com.app.test02;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

public class PopwindowLeftNew extends Activity{
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popupwindow_main);

findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 获取自定义布局文件activity_popupwindow_left.xml的视图
View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false);
// 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);
// 设置动画效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
// 这里是位置显示方式,在屏幕的左侧
popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
// 点击其他地方消失
popupWindow_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
}
});

}
}





附:一些相关的布局文件


PopupWindow弹出菜单

activity_popupwindow_main.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff" >

<Button android:id="@+id/popBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="弹出左侧菜单" />

</LinearLayout>
activity_popupwindow_left.xml 
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="50dp">

<Button
android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="打开" />

<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="保存" />

<Button
android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="关闭" />

<Button
android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="打开" />

<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="保存" />

<Button
android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="关闭" />

<Button
android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="打开" />

<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="保存" />

<Button
android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:text="关闭" />

</LinearLayout>



弹出动画XML

在res文件夹下,建立anim文件夹。写入如下两个文件。

弹出动画


in_lefttoright.xml 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- 定义从左向右进入的动画 -->
<translate
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0" />

</set>


弹回动画


out_righttoleft.xml 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- 定义从右向左动画退出动画 -->
<translate
android:duration="500"
android:fromXDelta="0"
android:toXDelta="-100%" />

</set>



动画管理

在styles.xml中,添加如下管理代码
<style name="AnimationFade">

<!-- PopupWindow左右弹出的效果 -->
<item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
<item name="android:windowExitAnimation">@anim/out_righttoleft</item>
</style>


今天由于项目需要学习了一点PopuWindow的基础知识

如何使用:

案例:

第一步创建泡泡窗口的布局:

popup_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="right"
android:orientation="vertical" >

<LinearLayout
android:layout_width="100dp"
android:layout_height="260dp"
android:background="@color/black"
android:orientation="vertical" >

<Button
android:id="@+id/open_location"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:text="开启定位"
android:textColor="@color/orange"
android:textSize="15sp" />

<Button
android:id="@+id/close_location"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:text="关闭定位"
android:textColor="@color/orange"
android:textSize="15sp" />

<Button
android:id="@+id/send_Message"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:text="一键通知"
android:textColor="@color/orange"
android:textSize="15sp" />
</LinearLayout>

</LinearLayout>


然后在MainActivity中添加显示popupwindow的方法:

private void showPopupWindow() {

contentView = LayoutInflater.from(this).inflate(R.layout.popup_setting, null);
PopupWindow mPopWindow = new PopupWindow(contentView,
dip2px(this, 100), dip2px(this, 260), true);
mPopWindow.setContentView(contentView);

//显示PopupWindow
View rootview = LayoutInflater.from(this).inflate(R.layout.activity_main, null);

mPopWindow.setOutsideTouchable(true);
mPopWindow.setBackgroundDrawable(new ColorDrawable(0));
mPopWindow.showAsDropDown(setting);
setPopupOnListener();//设置按钮事件监听

}
/**
* 将dp转换为sp
*
* @param context
* @param dpValue
* @return
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 设置popupwindow里面包含的控件的点击事件
*/
public void setPopupOnListener(){
Button open_location = (Button) contentView.findViewById(R.id.open_location);
Button close_location = (Button) contentView.findViewById(R.id.close_location);
Button sendMessage = (Button) contentView.findViewById(R.id.send_Message);
MainListenter listener = new MainListenter();

open_location.setOnClickListener(listener);
close_location.setOnClickListener(listener);
sendMessage.setOnClickListener(listener);
sendMessage.setOnClickListener(listener);
}


解释一下:


PopupWindow的相关函数


(1)、构造函数:

//方法一:
public PopupWindow (Context context)
//方法二:
public PopupWindow(View contentView)
//方法三:
public PopupWindow(View contentView, int width, int height)
//方法四:
public PopupWindow(View contentView, int width, int height, boolean focusable)

首要注意:看这里有四个构造函数,但要生成一个PopupWindow最基本的三个条件是一定要设置的:View contentView,int width, int height ;少任意一个就不可能弹出来PopupWindow!!!!
所以,如果使用方法一来构造PopupWindow,那完整的构造代码应该是这样的:

View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
PopupWindwo popWnd = PopupWindow (context);
popWnd.setContentView(contentView);
popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

由于方法三中,含有了这三个必备条件,不用单独设置contentview或者width、height,所以一般使用构造方法三


(2)显示函数

显示函数主要使用下面三个
<span style="font-size:14px;">//相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor):
//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
showAsDropDown(View anchor, int xoff, int yoff):
//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
showAtLocation(View parent, int gravity, int x, int y):</span>

这里有两种显示方式:
1、显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);
2、指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);


(3)、其它函数

public void dismiss()//将窗体隐藏
//另外几个函数,这里不讲其意义,下篇细讲
public void setFocusable(boolean focusable)//PopupWindow是否具有获取焦点的能力,默认为False。一般来讲是没有用的,因为普通的控件是不需要获取焦点的,而对于EditText则不同,如果不能获取焦点,那么EditText将是无法编辑的</span>
public void setTouchable(boolean touchable)//设置PopupWindow是否响应touch事件,如果设置为false,则布局里面包含的所有控件的touch事件无响应,包括点击事件)</span>
public void setOutsideTouchable(boolean touchable)//这个函数的意义,就是指,PopupWindow以外的区域是否可点击,即如果点击PopupWindow以外的区域,PopupWindow是否会消失。public void setBackgroundDrawable(Drawable
background)//加上它之后,setOutsideTouchable()才会生效;而且,只有加上它之后,PopupWindow才会对手机的返回按钮有响应:即,点击手机返回按钮,可以关闭PopupWindow;如果不加setBackgroundDrawable()将关闭的PopupWindow所在的Activity.,参数填充进去各种Drawable,比如new
BitmapDrawable(),new ColorDrawable(),等;



如何想实现点击popuwindow外部的区域或实现点击back键隐藏窗口,必须同时设置

mPopWindow.setOutsideTouchable(true);

mPopWindow.setBackgroundDrawable(new ColorDrawable(0));

这两个方法缺一不可

如何设置Popupwindow的大小

在Android开发过程中,经常使用PopupWindow控件来弹出菜单,但发现会出现不同尺寸显示屏的终端显示效果不一致的问题,弹出菜单的大小和位置都会发生变化。
        最有可能的原因是Android开发的xml布局文件使用单位dp以保证不同屏幕分辨率的机器上布局一致,但PopupWindow控件中控制大小和位置的参数所用的单位是px,所以如果两边使用相同的数值控制尺寸和位置,就会造成显示效果不一致。
        这种情况下必须进行单位的转换,在PopupWindow控件使用前将布局参数转换后再填入参数中。转换的函数参照如下:

    //根据手机的分辨率从
dp 的单位 转成为 px(像素)
    public
static int dip2px(Context context, float dpValue) { 
       final
float scale = context.getResources().getDisplayMetrics().density; 
       return
(int) (dpValue * scale + 0.5f);
    }



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: