本站已运行

攻城狮论坛

作者: 腾科2016
查看: 4031|回复: 0

主题标签Tag

more +今日重磅推荐Recommend No.1

所有IT类厂商认证考试题库下载所有IT类厂商认证考试题库下载

more +随机图赏Gallery

【新盟教育】2023最新华为HCIA全套视频合集【网工基础全覆盖】---国sir公开课合集【新盟教育】2023最新华为HCIA全套视频合集【网工基础全覆盖】---国sir公开课合集
【新盟教育】网工小白必看的!2023最新版华为认证HCIA Datacom零基础全套实战课【新盟教育】网工小白必看的!2023最新版华为认证HCIA Datacom零基础全套实战课
原创_超融合自动化运维工具cvTools原创_超融合自动化运维工具cvTools
重量级~~30多套JAVA就业班全套 视频教程(请尽快下载,链接失效后不补)重量级~~30多套JAVA就业班全套 视频教程(请尽快下载,链接失效后不补)
链接已失效【超过几百G】EVE 国内和国外镜像 全有了 百度群分享链接已失效【超过几百G】EVE 国内和国外镜像 全有了 百度群分享
某linux大佬,积累多年的电子书(约300本)某linux大佬,积累多年的电子书(约300本)
乾颐堂现任明教教主Python完整版乾颐堂现任明教教主Python完整版
乾颐堂 教主技术进化论 2018-2019年 最新31-50期合集视频(各种最新技术杂谈视频)乾颐堂 教主技术进化论 2018-2019年 最新31-50期合集视频(各种最新技术杂谈视频)
Python学习视频 0起点视频 入门到项目实战篇 Python3.5.2视频教程 共847集 能学102天Python学习视频 0起点视频 入门到项目实战篇 Python3.5.2视频教程 共847集 能学102天
约21套Python视频合集 核心基础视频教程(共310G,已压缩)约21套Python视频合集 核心基础视频教程(共310G,已压缩)
最新20180811录制 IT爱好者-清风羽毛 - 网络安全IPSec VPN实验指南视频教程最新20180811录制 IT爱好者-清风羽毛 - 网络安全IPSec VPN实验指南视频教程
最新20180807录制EVE开机自启动虚拟路由器并桥接物理网卡充当思科路由器最新20180807录制EVE开机自启动虚拟路由器并桥接物理网卡充当思科路由器

[电脑技巧] 【软件学习】☑Android中的PopupWindow详解

[复制链接]
查看: 4031|回复: 0
开通VIP 免金币+免回帖+批量下载+无广告

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:

· AlertDialog的位置固定,而PopupWindow的位置可以随意

· AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下

· showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移

· showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移

· showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

下面通过一个Demo讲解(解释看注释):

main.xml

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="fill_parent"  

4.     android:layout_height="fill_parent"  

5.     android:orientation="vertical" >  

6.   

7.     <TextView  

8.         android:layout_width="fill_parent"  

9.         android:layout_height="wrap_content"  

10.         android:text="@string/hello" />  

11.   

12.     <Button  

13.         android:id="@+id/button01"  

14.         android:layout_width="fill_parent"  

15.         android:layout_height="wrap_content"  

16.         android:text="以自己为Anchor,不偏移" />  

17.   

18.     <Button  

19.         android:id="@+id/button02"  

20.         android:layout_width="fill_parent"  

21.         android:layout_height="wrap_content"  

22.         android:text="以自己为Anchor,有偏移" />  

23.   

24.     <Button  

25.         android:id="@+id/button03"  

26.         android:layout_width="fill_parent"  

27.         android:layout_height="wrap_content"  

28.         android:text="以屏幕中心为参照,不偏移(正中间)" />  

29.   

30.     <Button  

31.         android:id="@+id/button04"  

32.         android:layout_width="fill_parent"  

33.         android:layout_height="wrap_content"  

34.         android:text="以屏幕下方为参照,下方中间" />  

35.   

36. </LinearLayout>  


. Z, a0 X( C2 v: Y) Vpopup_window.xml

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:layout_width="wrap_content"  

4.     android:layout_height="wrap_content"  

5.     android:background="#00FF00"  

6.     android:orientation="vertical" >  

7.   

8.     <TextView  

9.         android:layout_width="wrap_content"  

10.         android:layout_height="wrap_content"  

11.         android:text="选择状态:"  

12.         android:textColor="@android:color/white"  

13.         android:textSize="20px" />  

14.   

15.     <RadioGroup  

16.         android:id="@+id/radioGroup"  

17.         android:layout_width="wrap_content"  

18.         android:layout_height="wrap_content"  

19.         android:orientation="vertical" >  

20.   

21.         <RadioButton android:text="在线" />  

22.   

23.         <RadioButton android:text="离线" />  

24.   

25.         <RadioButton android:text="隐身" />  

26.     </RadioGroup>  

27.   

28. </LinearLayout>  


# O) O# w" u4 G3 jPopupWindowDemoActivity.java

1. package com.tianjf;  

2.   

3. import android.app.Activity;  

4. import android.os.Bundle;  

5. import android.view.Gravity;  

6. import android.view.LayoutInflater;  

7. import android.view.View;  

8. import android.view.View.OnClickListener;  

9. import android.widget.Button;  

10. import android.widget.PopupWindow;  

11. import android.widget.RadioGroup;  

12. import android.widget.RadioGroup.OnCheckedChangeListener;  

13.   

14. public class PopupWindowDemoActivity extends Activity implements OnClickListener,  

15.         OnCheckedChangeListener {  

16.   

17.     private Button mbutton01;  

18.     private Button mbutton02;  

19.     private Button mbutton03;  

20.     private Button mbutton04;  

21.     private PopupWindow mPopupWindow;  

22.     // 屏幕的width  

23.     private int mScreenWidth;  

24.     // 屏幕的height  

25.     private int mScreenHeight;  

26.     // PopupWindow的width  

27.     private int mPopupWindowWidth;  

28.     // PopupWindow的height  

29.     private int mPopupWindowHeight;  

30.   

31.     @Override  

32.     public void onCreate(Bundle savedInstanceState) {  

33.         super.onCreate(savedInstanceState);  

34.         setContentView(R.layout.main);  

35.   

36.         mbutton01 = (Button) findViewById(R.id.button01);  

37.         mbutton02 = (Button) findViewById(R.id.button02);  

38.         mbutton03 = (Button) findViewById(R.id.button03);  

39.         mbutton04 = (Button) findViewById(R.id.button04);  

40.   

41.         mbutton01.setOnClickListener(this);  

42.         mbutton02.setOnClickListener(this);  

43.         mbutton03.setOnClickListener(this);  

44.         mbutton04.setOnClickListener(this);  

45.     }  

46.   

47.     @Override  

48.     public void onClick(View v) {  

49.         switch (v.getId()) {  

50.         // 相对某个控件的位置(正左下方),无偏移  

51.         case R.id.button01:  

52.             getPopupWindowInstance();  

53.             mPopupWindow.showAsDropDown(v);  

54.             break;  

55.   

56.         // 相对某个控件的位置(正左下方),有偏移  

57.         case R.id.button02:  

58.             getPopupWindowInstance();  

59.             mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50  

60.             break;  

61.   

62.         // 相对于父控件的位置,无偏移  

63.         case R.id.button03:  

64.             getPopupWindowInstance();  

65.             mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);  

66.             break;  

67.   

68.         // 相对于父控件的位置,有偏移  

69.         case R.id.button04:  

70.             getPopupWindowInstance();  

71.             mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);  

72.             break;  

73.   

74.         default:  

75.             break;  

76.         }  

77.     }  

78.   

79.     @Override  

80.     public void onCheckedChanged(RadioGroup group, int checkedId) {  

81.         mPopupWindow.dismiss();  

82.     }  

83.   

84.     /*

85.      * 获取PopupWindow实例

86.      */  

87.     private void getPopupWindowInstance() {  

88.         if (null != mPopupWindow) {  

89.             mPopupWindow.dismiss();  

90.             return;  

91.         } else {  

92.             initPopuptWindow();  

93.         }  

94.     }  

95.   

96.     /*

97.      * 创建PopupWindow

98.      */  

99.     private void initPopuptWindow() {  

100.         LayoutInflater layoutInflater = LayoutInflater.from(this);  

101.         View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);  

102.         RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);  

103.         radioGroup.setOnCheckedChangeListener(this);  

104.   

105.         // 创建一个PopupWindow  

106.         // 参数1:contentView 指定PopupWindow的内容  

107.         // 参数2:width 指定PopupWindow的width  

108.         // 参数3:height 指定PopupWindow的height  

109.         mPopupWindow = new PopupWindow(popupWindow, 100, 130);  

110.   

111.         // 获取屏幕和PopupWindow的width和height  

112.         mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();  

113.         mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();  

114.         mPopupWindowWidth = mPopupWindow.getWidth();  

115.         mPopupWindowHeight = mPopupWindow.getHeight();  

116.     }  

117. }  

1 O% g% Y9 f. m8 D' o3 |) V) B
CCNA考试 官方正规报名 仅需1500元
回复 论坛版权

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|无图浏览|手机版|网站地图|攻城狮论坛

GMT+8, 2026-7-5 16:12 , Processed in 0.106370 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4 © 2001-2013 Comsenz Inc.

Designed by ARTERY.cn