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

安卓开发笔记——PopupWindow,做出如弹出框效果

2016-03-29 00:50 363 查看
先看一个效果图


点击按钮后出现一个这么的效果,这个弹出框实现的答题代码如下

先来一个弹出框的布局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="#90EE90" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"

android:text="请选择"
android:textColor="#ff2525"
android:textSize="20sp" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#969696" />

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:onClick="abc1"
android:gravity="left|center_vertical"
android:padding="10dp"
android:text="个人信息"
android:textColor="#2a2a2a"
android:textSize="15sp" />

<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#a6a6a6" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:onClick="abc2"
android:gravity="left|center_vertical"
android:padding="10dp"
android:text="好友列表"
android:textColor="#2a2a2a"
android:textSize="15sp" />

<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#a6a6a6" />

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:onClick="abc2"
android:gravity="left|center_vertical"
android:padding="10dp"
android:text="我的留言板"
android:textColor="#2a2a2a"
android:textSize="15sp" />

</LinearLayout>

</LinearLayout>


然后是在主界面的设计,按钮自己设定,我把在按钮内如何引入写出来

代码如下

LayoutInflater inflater = LayoutInflater.from(getBaseContext());//获取一个填充器
View view = inflater.inflate(R.layout.chakan, null);//填充我们自定义的布局
Display display = getWindowManager().getDefaultDisplay();//得到当前屏幕的显示器对象
Point size = new Point();//创建一个Point点对象用来接收屏幕尺寸信息
display.getSize(size);//Point点对象接收当前设备屏幕尺寸信息
int width = size.x;//从Point点对象中获取屏幕的宽度(单位像素)
int height = size.y;//从Point点对象中获取屏幕的高度(单位像素)
Log.v("zxy", "width="+width+",height="+height);//width=480,height=854可知手机的像素是480x854的
//创建一个PopupWindow对象,第二个参数是设置宽度的,用刚刚获取到的屏幕宽度乘以2/3,取该屏幕的2/3宽度,从而在任何设备中都可以适配,高度则包裹内容即可,最后一个参数是设置得到焦点
PopupWindow popWindow = new PopupWindow(view, 2*width/3, LayoutParams.WRAP_CONTENT, true);
popWindow.setBackgroundDrawable(new BitmapDrawable());//设置PopupWindow的背景为一个空的Drawable对象,如果不设置这个,那么PopupWindow弹出后就无法退出了
popWindow.setOutsideTouchable(true);//设置是否点击PopupWindow外退出PopupWindow
WindowManager.LayoutParams params = getWindow().getAttributes();//创建当前界面的一个参数对象
params.alpha = 1f;//设置参数的透明度为0.8,透明度取值为0~1,1为完全不透明,0为完全透明,因为android中默认的屏幕颜色都是纯黑色的,所以如果设置为1,那么背景将都是黑色,设置为0,背景显示我们的当前界面
getWindow().setAttributes(params);//把该参数对象设置进当前界面中

Button btn = (Button) view.findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(user.this,persondata.class);
startActivity(intent);
finish();
}
});

Button btn1 = (Button) view.findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg1) {
Intent intent=new Intent(user.this,friendlist.class);
startActivity(intent);
finish();
}
});

Button btn2 = (Button) view.findViewById(R.id.button3);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg1) {
Intent intent=new Intent(user.this,liuyanban.class);
startActivity(intent);
finish();
}
});

popWindow.setOnDismissListener(new OnDismissListener() {//设置PopupWindow退出监听器
@Override
public void onDismiss() {//如果PopupWindow消失了,即退出了,那么触发该事件,然后把当前界面的透明度设置为不透明
WindowManager.LayoutParams params = getWindow().getAttributes();
params.alpha = 1.0f;//设置为不透明,即恢复原来的界面
getWindow().setAttributes(params);
}
});
//第一个参数为父View对象,即PopupWindow所在的父控件对象,第二个参数为它的重心,后面两个分别为x轴和y轴的偏移量
popWindow.showAtLocation(inflater.inflate(R.layout.addfriend_layout, null), Gravity.CENTER, 0, 0);


代码里的解释已经很详尽了,这也是我学着写的 ,然后弹出框的点击事件这样写就可以了,千万不要在xml里直接使用android:onClick=“click1”这样,然后直接在主界面里,用

public void click1(View v){}这样写,因为view指的并不是弹出框的布局xml,因此不会做出响应
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: