- 浏览: 934135 次
- 性别:
- 来自: 山西
文章分类
最新评论
-
白小默:
你好 可以提供下源码DEMO吗,不知为何,我导出来的excel ...
jxls 使用模板文件导出生成excel -
zkzqzzz:
博主威武!
让微信二维码扫描您的APK -
zkzqzzz:
感谢博主 原来那些类都不是必须的 或者自己写!!博主真棒 ...
抢红包插件实现原理浅析 -
zkzqzzz:
博主 请问你的其他类在哪里呢?
抢红包插件实现原理浅析 -
zkzqzzz:
其他类在哪呢?
抢红包插件实现原理浅析
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
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/button01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="以自己为Anchor,不偏移" />
- <Button
- android:id="@+id/button02"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="以自己为Anchor,有偏移" />
- <Button
- android:id="@+id/button03"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="以屏幕中心为参照,不偏移(正中间)" />
- <Button
- android:id="@+id/button04"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="以屏幕下方为参照,下方中间" />
- </LinearLayout>
popup_window.xml
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00FF00"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="选择状态:"
- android:textColor="@android:color/white"
- android:textSize="20px" />
- <RadioGroup
- android:id="@+id/radioGroup"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <RadioButton android:text="在线" />
- <RadioButton android:text="离线" />
- <RadioButton android:text="隐身" />
- </RadioGroup>
- </LinearLayout>
PopupWindowDemoActivity.java
[java] view plaincopy
- package com.tianjf;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.PopupWindow;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- public class PopupWindowDemoActivity extends Activity implements OnClickListener,
- OnCheckedChangeListener {
- private Button mbutton01;
- private Button mbutton02;
- private Button mbutton03;
- private Button mbutton04;
- private PopupWindow mPopupWindow;
- // 屏幕的width
- private int mScreenWidth;
- // 屏幕的height
- private int mScreenHeight;
- // PopupWindow的width
- private int mPopupWindowWidth;
- // PopupWindow的height
- private int mPopupWindowHeight;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mbutton01 = (Button) findViewById(R.id.button01);
- mbutton02 = (Button) findViewById(R.id.button02);
- mbutton03 = (Button) findViewById(R.id.button03);
- mbutton04 = (Button) findViewById(R.id.button04);
- mbutton01.setOnClickListener(this);
- mbutton02.setOnClickListener(this);
- mbutton03.setOnClickListener(this);
- mbutton04.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- // 相对某个控件的位置(正左下方),无偏移
- case R.id.button01:
- getPopupWindowInstance();
- mPopupWindow.showAsDropDown(v);
- break;
- // 相对某个控件的位置(正左下方),有偏移
- case R.id.button02:
- getPopupWindowInstance();
- mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50
- break;
- // 相对于父控件的位置,无偏移
- case R.id.button03:
- getPopupWindowInstance();
- mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
- break;
- // 相对于父控件的位置,有偏移
- case R.id.button04:
- getPopupWindowInstance();
- mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);
- break;
- default:
- break;
- }
- }
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- mPopupWindow.dismiss();
- }
- /*
- * 获取PopupWindow实例
- */
- private void getPopupWindowInstance() {
- if (null != mPopupWindow) {
- mPopupWindow.dismiss();
- return;
- } else {
- initPopuptWindow();
- }
- }
- /*
- * 创建PopupWindow
- */
- private void initPopuptWindow() {
- LayoutInflater layoutInflater = LayoutInflater.from(this);
- View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);
- RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);
- radioGroup.setOnCheckedChangeListener(this);
- // 创建一个PopupWindow
- // 参数1:contentView 指定PopupWindow的内容
- // 参数2:width 指定PopupWindow的width
- // 参数3:height 指定PopupWindow的height
- mPopupWindow = new PopupWindow(popupWindow, 100, 130);
- // 获取屏幕和PopupWindow的width和height
- mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
- mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();
- mPopupWindowWidth = mPopupWindow.getWidth();
- mPopupWindowHeight = mPopupWindow.getHeight();
- }
- }
发表评论
-
Android客户端代码保护技术-完整性校验
2018-01-08 17:10 1252由于Android系统固有的缺陷、Android应用分发 ... -
android开发中手动v2签名实现方法
2018-01-08 10:31 1000用v2签名前要进行v1签名或zipalign。 1. z ... -
webview支持LocalStorage本地存储
2017-03-31 10:30 1362//允许JavaScript执行 ... -
七步配置phonegap+cordova+ionic开发环境
2016-12-19 16:21 691本文讲的是Android混合 ... -
梆梆加固破解
2016-10-13 09:01 1500本文假设你已经使用过梆梆加固,手头至少有个一个通过梆梆加固后 ... -
android zxing 扫描二维码 横屏转竖屏
2016-09-14 15:41 0Zxing 竖屏切换 Android 在google上下 ... -
封装Volley使Volley的每个请求都自动保存和发送Cookie
2016-09-14 10:57 0思路很简单,每次请求获取到服务器返回的response就解 ... -
Android二维码ZXing扫描区域大小的调整,提高扫描速度
2016-09-14 10:48 2638Zxing本身默认的扫图区域最大只有 360*480 ... -
解决Android二维码扫描ZXing竖屏拉伸变长闪退扫描区域小等问题
2016-09-14 10:42 1222Android 基于google Zxing实现二维码、条形 ... -
Android平台下利用zxing实现二维码开发
2016-09-14 09:25 478现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研 ... -
Zxing扫描二维码
2016-09-14 09:24 653摘要 android Zxing扫描二维码 横竖屏 ... -
Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
2016-09-14 09:18 499转载请注明出处:http://blog.csdn.net/x ... -
React Native应用部署/热更新-CodePush最新集成总结
2016-08-02 11:06 563本文出自《React Native学习笔记》系列文章。 ... -
让微信二维码扫描您的APK
2016-07-27 11:26 1498二维码深入人心,很多App都在官网挂出了可以扫描下载apk的 ... -
Activity类的runOnUiThread方法
2016-07-22 10:40 765[javascript] view plain c ... -
Can't create handler inside thread that has not called Looper.prepare()
2016-07-22 10:24 625错误信息:在Android开发中,如果在一个Thread中启 ... -
Android编程之解决android-support-v4打包问题
2016-07-21 09:27 548如果工程引入了android-support-v4的jar类 ... -
Android 编程下的代码混淆之(android-support-v4.jar)
2016-07-21 09:24 392项目在代码混淆过程中如果引用了第三方 Jar 包,需要在混淆 ... -
phoneGap可行性分析
2016-07-20 16:25 5101 移动应用现状 移动应用产品往往常 ... -
梆梆SDKs详细分析(1) - 防界面劫持SDK
2016-07-20 10:23 747前言 前段时间,国外知名的安全公司fireeye发 ...
相关推荐
主要为大家详细介绍了Android中PopupWindow使用方法,感兴趣的小伙伴们可以参考一下
Android PopupWindow的使用详解 http://blog.csdn.net/yalinfendou博客中有详细介绍……
本篇文章主要介绍了详解Android中PopupWindow在7.0后适配的解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
android ICS式下拉菜单实现详解 PopupWindow
主要为大家详细介绍了Android组件popupwindow使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
博客《 PopUpWindow使用详解(一)——基本使用》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/49272285
主要为大家详细介绍了Android下拉框PopupWindow的使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本篇文章主要介绍了详解Android PopupWindow怎么合理控制弹出位置(showAtLocation),具有一定的参考价值,有兴趣的可以了解一下
博客《PopUpWindow使用详解(二)——进阶及答疑》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/49278705
本文实例为大家分享了android popupwindow的用法,供大家参考,具体内容如下 一、基本用法 一般做法,新建类继承popupwindow。例 /** * popupwindow基本用法 * Created by Administrator on 2015/11/25. */ ...
主要介绍了 Android PopupWindow全屏详细介绍及实例代码的相关资料,需要的朋友可以参考下
最近想要做一个弹出更多的菜单,而原生的弹出菜单却不是我们想要的效果,所以必然要自定义菜单。接下来通过本文给大家介绍android 使用popupwindow实现弹出更多的菜单实例详解,需要的朋友可以参考下
主要介绍了详解Android中提示对话框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)的相关资料,需要的朋友可以参考下
主要介绍了Android开发之PopupWindow创建弹窗、对话框的方法,结合实例形式详细分析了Android使用PopupWindow创建对话框相关操作技巧,需要的朋友可以参考下