- 浏览: 325814 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
xuanyuanxiaoxue:
...
Android - LayoutInflater -
柴兴博:
不错 多谢
Android 悬浮Activity并可拖动(访悬浮歌词) -
di1984HIT:
写的很好,我收藏一下。
java之动态代理模式(JDK和cglib) -
chinacssnj:
待测试,明天测,测试的结果发给大家
网络开发上传文件到服务器 -
fx_199182:
...
Android之MediaPlayer
Android之PopupWindow弹出对话框
- 博客分类:
- Android之控件及布局
Android 的 对话框常用的有两种:PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,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等 |
下面是运行程序截图:
程序代码:
布局:main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" Android:id="@+id/layout" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <TextView Android:id="@+id/tv_showText" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:gravity="center" Android:text="@string/hello" Android:textSize="22px" /> <Button Android:id="@+id/bt_PopupWindow1" Android:text="以自己为Anchor,不偏移" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> <Button Android:id="@+id/bt_PopupWindow2" Android:text="以自己为Anchor,正下方" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> <Button Android:id="@+id/bt_PopupWindow3" Android:text="以屏幕中心为参照,不偏移(正中间)" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> <Button Android:id="@+id/bt_PopupWindow4" Android:text="以屏幕下方为参照,下方中间" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> </LinearLayout>
自定义对话框dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <TextView Android:id="@+id/tv_tip" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="请输入内容:" /> <EditText Android:id="@+id/et_text" Android:layout_width="fill_parent" Android:layout_height="wrap_content" ></EditText> <LinearLayout Android:gravity="center_horizontal" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <Button Android:id="@+id/bt_ok" Android:text="确定" Android:layout_width="100px" Android:layout_height="50px" /> <Button Android:id="@+id/bt_cancle" Android:text="取消" Android:layout_width="100px" Android:layout_height="50px" /> </LinearLayout> </LinearLayout>
下面是详细代码:
package com.myAndroid.test; import Android.app.Activity; import Android.content.Context; import Android.content.SharedPreferences.Editor; 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.ViewGroup.LayoutParams; import Android.widget.Button; import Android.widget.EditText; import Android.widget.Gallery; import Android.widget.PopupWindow; import Android.widget.TextView; public class PopupWindowTest extends Activity {//PopupWindow属于不阻塞的对话框,AlertDialog则是阻塞的。 private Button bt_popupWindow1; private Button bt_popupWindow2; private Button bt_popupWindow3; private Button bt_popupWindow4; private TextView tv_showText; private PopupWindow popupWindow; private int screenWidth; private int screenHeight; private int dialgoWidth; private int dialgoheight; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } /** * 初始化控件和响应事件 */ private void initView() { bt_popupWindow1 = (Button)findViewById(R.id.bt_PopupWindow1); bt_popupWindow2 = (Button)findViewById(R.id.bt_PopupWindow2); bt_popupWindow3 = (Button)findViewById(R.id.bt_PopupWindow3); bt_popupWindow4 = (Button)findViewById(R.id.bt_PopupWindow4); tv_showText = (TextView)findViewById(R.id.tv_showText); bt_popupWindow1.setOnClickListener(new ClickEvent()); bt_popupWindow2.setOnClickListener(new ClickEvent()); bt_popupWindow3.setOnClickListener(new ClickEvent()); bt_popupWindow4.setOnClickListener(new ClickEvent()); } /** * 按钮点击事件处理 * @author Kobi * */ private class ClickEvent implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.bt_PopupWindow1: //以自己为Anchor,不偏移 getPopupWindow(); popupWindow.showAsDropDown(v); break; case R.id.bt_PopupWindow2: //以自己为Anchor,偏移(screenWidth-dialgoWidth)/2, 0)--按钮正下方 getPopupWindow(); popupWindow.showAsDropDown(v, (screenWidth-dialgoWidth)/2, 0); break; case R.id.bt_PopupWindow3: //以屏幕中心为参照,不偏移 getPopupWindow(); popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.CENTER, 0, 0); break; case R.id.bt_PopupWindow4: //以屏幕左下角为参照,偏移(screenWidth-dialgoWidth)/2, 0) --屏幕下方中央 getPopupWindow(); popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.BOTTOM, 0, 0); break; default: break; } } } /** * 创建PopupWindow */ protected void initPopuptWindow() { // TODO Auto-generated method stub View popupWindow_view = getLayoutInflater().inflate( //获取自定义布局文件dialog.xml的视图 R.layout.dialog, null,false); popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);//创建PopupWindow实例 Button bt_ok = (Button)popupWindow_view.findViewById(R.id.bt_ok); //dialog.xml视图里面的控件 Button bt_cancle = (Button)popupWindow_view.findViewById(R.id.bt_cancle); final EditText et_text = (EditText)popupWindow_view.findViewById(R.id.et_text); bt_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv_showText.setText(et_text.getText()); //在标签里显示内容 popupWindow.dismiss(); //对话框消失 } }); bt_cancle.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub popupWindow.dismiss(); } }); //获取屏幕和对话框各自高宽 screenWidth = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth(); screenHeight = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight(); dialgoWidth = popupWindow.getWidth(); dialgoheight = popupWindow.getHeight(); } /* * 获取PopupWindow实例 */ private void getPopupWindow() { if(null != popupWindow) { popupWindow.dismiss(); return; }else { initPopuptWindow(); } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.e("ActivityState", "onPause"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.e("ActivityState", "onResume"); } }
发表评论
-
Android 漂亮的 Dialog (弹出框)
2012-02-13 23:35 4212在新浪微博Android客户端的开发的课程中呢要用的得自定义的 ... -
ListView的分页显示
2012-01-13 11:22 1208package com.anddev.ListMore.Tes ... -
ProgressBar的圆形和长条形设置
2012-01-12 15:02 0默认的progresbar是圆形的旋转动画: <Pro ... -
把TextView中的文字添加阴影效果及Style的使用
2012-01-12 15:00 2066<TextView android:id=" ... -
GridView动态加载数据情况下,选中状态的实现
2012-01-12 14:57 1460GridView使用AdapterView动态加载数据情况下, ... -
Android中的透明属性的设置
2012-01-12 14:50 1249ui设计时,需要指定某个元素的drawable时,有时候需要告 ... -
简单实现ImageView的加边框颜色的方法
2012-01-12 14:48 2327对于一个ImageView,如果我们要加上边框,比如宽为3dp ... -
android ,点击title的ImageView弹出PopupWindow【安卓进化七】
2011-12-03 10:33 1404一、曾经做过一个项目,用的大概就是这种布局,title布局,t ... -
CursorTreeAdapter 详解
2011-12-02 17:44 2183CursorTreeAdapter 通过该适配类可以用一连续的 ... -
Android 应用程序窗体显示状态操作(requestWindowFeature()的应用)
2011-12-02 09:43 755我们在开发程序是经常会需要软件全屏显示、自定义标题(使用按钮等 ... -
Android GestureDetector 手势基础
2011-12-01 16:34 12781. 当用户触摸屏幕的时候,会产生许多手势,例如dow ... -
Android GestureDetector手势识别类
2011-12-01 14:26 2763为了加强鼠标响应事件,Android提供了GestureDet ... -
android之Tabhost深入讲解二
2011-11-29 09:18 2659之前一直认为TabHost不够灵活而使用Activit ... -
android之Tabhost深入讲解一
2011-11-28 21:46 1678大家都知道Tabhost中Tabwidget的布局是图标在上, ... -
Android中的view
2011-11-21 15:02 1148View类是Android的一个超类,这个类几乎包含了所有的屏 ... -
Android自定义View之一:初探实例 .
2011-11-21 14:53 800Android自定义View实现很简单 继承View,重写构 ...
相关推荐
PopupWindow之显示顶层对话框代码
分享一个在android开发中的多级PopupWindow 弹出窗口实例,不知道这种多级的弹出窗口算不算是“嵌套”方式?因为它们之间确实是有着层级的关系,当PopupWindow被主界面按钮触发的时候,会被弹出第一级,在第一级中...
在使用UC-WebBrowser时,你会发现它的弹出菜单跟系统自带的菜单不一样。它实现更多菜单选项的显示和分栏。其实,它的本身是PopupWindow或者是AlertDialog对话框,在里面添加两个GridView控件,一个是菜单标题栏,一...
自定义单选、多选对话框及popupwindow窗口实例源码是一个自定义的弹出对话框例子源码,弹出的时候有半透明效果,提供的有弹出单选(popwindow)、弹出多选(popwindow)、弹出单选(dialog)、弹出多选(dialog)等几种实现...
Android安卓用Dialog对话框简单并且完美实现popupWindow底部弹出效果,有动画效果
具体使用请关注博客:http://blog.csdn.net/stoppig 查看《Android中popWindow弹出菜单的编写》这篇文章
最近在使用IOS系统的过程中发现IOS底部弹出框甚是漂亮,大气,上档次,于是乎就想啊能不能在Android中实现类似的对话框呢?你说,这不是废话吗,除了一些极少数的系统级的不能模仿外(版权)还有啥不能依瓢画葫芦的...
Android PopupWindow 弹出菜单
其实,它的本身是PopupWindow或者是AlertDialog对话框,在里面添加两个GridView控件,一个是菜单标题栏,一个是菜单选项。菜单选项视图的切换可以通过适配器的变换,轻松地实现。点击下载该实例:一、运行截图: ...
对应博文:http://blog.csdn.net/iwanghang/article/details/53763364
本例子是一个自定义的弹出对话框例子源码,弹出的时候有半透明效果,提供的有弹出单选(popwindow)、弹出多选(popwindow)、弹出单选(dialog)、弹出多选(dialog)等几种实现形式,点击监听都在例子里面写好了如果有这...
PopupWindowMenuIM 基于RecyclerView实现的仿IM聊天界面,item长按根据触摸位置弹出对话框 ;; see . 由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见! Hello , World ! 感谢所有!
在Android开发中,经常需要通过点击某个按钮弹出对话框或者选择框,通过Dialog或者PopupMenu、PopupWindow都能实现。 这里主要介绍后两者:PopupMenu、PopupWindow的实现。 先看两个效果图上边PopupMenu,下边...
主要介绍了Android中自定义PopupWindow实现弹出框并带有动画效果的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
主要介绍了详解Android中提示对话框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)的相关资料,需要的朋友可以参考下
界面效果如图所示,点击 Click 按钮后,弹出对话框提示。 (1). CustomDailog的布局 首先定义 CustDailog的布局文件,由系统的AlertDailog可以知道,一个对话框包含了三个要素,一个是Title
一个Android登录界面的制作范例,以Android dialog方式实现的弹出式登录框,并包括了帐号密码保存、加密算法等模块源码。本登录源码将对密码信息进行AES加密算法加密后保存,有些功能需要服务器配合才能看到,比如...
微信朋友圈的点赞和评论功能,有2个组成部分:左下角的“更多”按钮;点击该按钮后弹出的对话框。这篇文章主要介绍了Android中使用PopupWindow 仿微信点赞和评论弹出,需要的朋友可以参考下
不会使宿主activity组件失去焦点,也就是说PopupWindow弹出后,你可以与宿主activity进行交互,Dialog却不能做到这一点。 注意:PopupWindow组件的使用问题,PopupWindow是一个阻塞对话框,如果你直接在Activity创建...
一个应用程序里面有多个activity和不同的对话框窗体,在我脑海深处的记忆力,对话框的使用一直都是new 出不同的dialog实例然后调用show 、dismiss方法,而弹出视图的控件监听很多时候都是在用CallBack回调函数处理,...