底部弹出popupwindow,背景变为半透明效果,采用两种方式实现
先来看看运行效果图
[方式一]实现从底部弹出popupwindow
原理:定义一个高度为wrap_content的popupwindow布局文件,根据屏幕底部的位置显示在bottom
1.首先定义一个高度为wrap_content的popupwindow布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<?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= "wrap_content"
android:gravity= "center"
android:padding= "8dp"
android:orientation= "vertical" >
<linearlayout
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:gravity= "center"
android:orientation= "vertical"
android:background= "@drawable/shape_info_bg" >
<textview android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:padding= "8dp"
android:gravity= "center"
android:textcolor= "@color/theme_blue"
android:text= "拍照" />
<view android:layout_width= "match_parent"
android:layout_height= "0.2dp"
android:background= "@color/theme_blue" />
<textview android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:padding= "8dp"
android:gravity= "center"
android:textcolor= "@color/theme_blue"
android:text= "相冊" />
</linearlayout>
<relativelayout
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_margintop= "5dp"
android:padding= "1dp" >
<button
android:id= "@+id/btn_cancle"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:gravity= "center"
android:padding= "8dp"
android:textcolor= "@color/theme_blue"
android:background= "@drawable/shape_info_bg"
android:text= "取消" />
</relativelayout>
</linearlayout>
|
2.在popupwindow所在的activity根布局中声明id(在弹出popupwindow作为位置参考的相对view)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+id/root_main"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical"
android:background= "@color/white" >
<button android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:text= "底部弹出popupwindow"
android:onclick= "openpop" />
<button android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:text= "半透明底部popupwindow"
android:onclick= "openpop2" />
</linearlayout>
|
3.mainactivity中点击按钮弹出popupwindow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** 弹出底部对话框 */
public void openpop(view view) {
view popview = layoutinflater.from(mcontext).inflate(
r.layout.pop_bottom, null);
view rootview = findviewbyid(r.id.root_main); // 當前頁面的根佈局
final popupwindow popupwindow = new popupwindow(popview, layoutparams.match_parent,layoutparams.wrap_content);
setbackgroundalpha(0.5f); //设置屏幕透明度
popupwindow.setbackgrounddrawable( new bitmapdrawable());
popupwindow.setfocusable( true ); // 点击空白处时,隐藏掉pop窗口
// 顯示在根佈局的底部
popupwindow.showatlocation(rootview, gravity.bottom | gravity.left, 0,
0);
}
|
4.设置背景变为半透明效果,由于popupwindow中并未设置背景色,所以弹出时popupwindow以外的部分显示当前acitivity的内容,设置背景色原理通过设置当前activity所在屏幕的透明度来达到背景透明的效果!在退出时popupwindow时,需要恢复屏幕原有透明度
1
2
3
4
5
6
7
|
popupwindow.setondismisslistener( new ondismisslistener() {
@override
public void ondismiss() {
// popupwindow隐藏时恢复屏幕正常透明度
setbackgroundalpha(1.0f);
}
});
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* 设置添加屏幕的背景透明度
*
* @param bgalpha
* 屏幕透明度0.0-1.0 1表示完全不透明
*/
public void setbackgroundalpha( float bgalpha) {
windowmanager.layoutparams lp = ((activity) mcontext).getwindow()
.getattributes();
lp.alpha = bgalpha;
((activity) mcontext).getwindow().setattributes(lp);
}
|
[方式二]全屏popupwindow实现底部弹出,背景半透明
原理:弹出一个全屏popupwindow,高度为match_parent,将根布局的gravity属性设置bottom,根布局背景设置为一个半透明的颜色#36000000, 弹出时全屏的popupwindow半透明效果背景覆盖了在activity之上 给人感觉上是popupwindow弹出后,背景变成半透明
布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<?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:padding= "8dp"
android:orientation= "vertical"
android:background= "#36000000"
android:gravity= "bottom" >
<linearlayout
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:gravity= "center"
android:orientation= "vertical"
android:background= "@drawable/shape_info_bg" >
<textview android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:padding= "8dp"
android:gravity= "center"
android:textcolor= "@color/theme_blue"
android:text= "拍照" />
<view android:layout_width= "match_parent"
android:layout_height= "0.2dp"
android:background= "@color/theme_blue" />
<textview android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:padding= "8dp"
android:gravity= "center"
android:textcolor= "@color/theme_blue"
android:text= "相冊" />
</linearlayout>
<relativelayout
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_margintop= "5dp"
android:padding= "1dp" >
<button
android:id= "@+id/btn_cancle"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:gravity= "center"
android:padding= "8dp"
android:textcolor= "@color/theme_blue"
android:background= "@drawable/shape_info_bg"
android:text= "取消" />
</relativelayout>
</linearlayout>
|
弹出方法和方式一相似,不过不用再监听popupwindow的退出事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/**
* 弹出底部对话框,达到背景背景透明效果
*
* 实现原理:弹出一个全屏popupwindow,将gravity属性设置bottom,根背景设置为一个半透明的颜色,
* 弹出时popupwindow的半透明效果背景覆盖了在activity之上 给人感觉上是popupwindow弹出后,背景变成半透明
*/
public void openpop2(view view) {
view popview = layoutinflater.from(mcontext).inflate(
r.layout.pop_bottom_alpha, null);
view rootview = findviewbyid(r.id.root_main); // 當前頁面的根佈局
final popupwindow popupwindow = new popupwindow(popview,
layoutparams.match_parent, layoutparams.match_parent);
// 设置弹出动画
popupwindow.setanimationstyle(r.style.animationfadebottom);
popupwindow.setbackgrounddrawable( new bitmapdrawable());
// 顯示在根佈局的底部
popupwindow.showatlocation(rootview, gravity.bottom | gravity.left, 0,
0);
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/LANG791534167/article/details/48985101