1、什么是popWindow?

popWindow这是对话的方式!文字解说android的方式来使用对话框,这就是所谓的popWindow。

2、popWindow特征

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

Dialog的位置固定,而PopupWindow的位置能够任意。

Dialog是非堵塞线程的,而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等),能够设置偏移或无偏移

3、popWindow的设计

效果图:

实现这个效果的,总共同拥有两个三个xml布局,一个是工具栏布局。一个是popWindow布局,一个是父界面布局。

工具栏布局:

<?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="@dimen/chat_llyt_title_height"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/black"
android:orientation="horizontal" > <ImageView
android:id="@+id/app_iv_back"
android:layout_width="@dimen/chat_llyt_title_height"
android:layout_height="fill_parent"
android:background="@drawable/actionbar_bg_left"
android:layout_gravity="center"
android:padding="7dip"
android:scaleType="centerInside"
/> <TextView
android:id="@+id/app_tv_barname"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="4.0"
android:background="@drawable/actionbar_bg_middle"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="@dimen/wc_title_textsize" /> <ImageView
android:id="@+id/app_iv_search"
android:layout_width="@dimen/chat_llyt_title_height"
android:layout_height="fill_parent"
android:background="@drawable/actionbar_bg_right"
android:padding="7dip"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="@drawable/yuyou_bg_pop" /> </LinearLayout>

popWindow布局:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlyt_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="9" /> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" > <View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.05" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.3"
android:background="@drawable/yuyou_pop_menu"
android:orientation="vertical" > <View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1" /> <LinearLayout
android:id="@+id/rlyt_code"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" > <ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:contentDescription="@drawable/face_code_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="@drawable/face_code_bg"
android:scaleType="centerInside" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
android:text="扫一扫"
android:textColor="@color/white"
android:textSize="@dimen/yuyou_pop_text_size" />
</LinearLayout> <View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.05"
android:visibility="gone"/> <LinearLayout
android:id="@+id/rlyt_his"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" > <ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:contentDescription="@drawable/face_his_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="@drawable/face_his_bg"
android:scaleType="centerInside" /> <TextView
android:id="@+id/tv_ting_tong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
android:text="历史记录"
android:textColor="@color/white"
android:textSize="@dimen/yuyou_pop_text_size" />
</LinearLayout> <View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.05"
android:visibility="gone"/>
</LinearLayout> <View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="6" />
</LinearLayout> <View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.05" /> </LinearLayout>

父界面布局:

<?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="@dimen/chat_llyt_title_height"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/black"
android:orientation="horizontal" > <ImageView
android:id="@+id/app_iv_back"
android:layout_width="@dimen/chat_llyt_title_height"
android:layout_height="fill_parent"
android:background="@drawable/actionbar_bg_left"
android:layout_gravity="center"
android:padding="7dip"
android:scaleType="centerInside"
/> <TextView
android:id="@+id/app_tv_barname"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="4.0"
android:background="@drawable/actionbar_bg_middle"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="@dimen/wc_title_textsize" /> <ImageView
android:id="@+id/app_iv_search"
android:layout_width="@dimen/chat_llyt_title_height"
android:layout_height="fill_parent"
android:background="@drawable/actionbar_bg_right"
android:padding="7dip"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="@drawable/yuyou_bg_pop" /> </LinearLayout>

对话框类,继承OnClickListener。对点击事件的对应:

package com.example.popwindow;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow; public class MySimplePopMenu implements OnClickListener {
private final PopupWindow popupWindow;
private Activity mParent; public MySimplePopMenu(Activity mParent, int hight) {
this.mParent = mParent;
View menu = initMenuView(mParent);
popupWindow = new PopupWindow(menu);
popupWindow.setWidth(LayoutParams.MATCH_PARENT);
popupWindow.setHeight(hight);
popupWindow.setAnimationStyle(R.style.PopAnimation);
} /**
* 设置详细Menubutton的监听
*
* @param mParent
* @return
*/
private View initMenuView(Activity mParent) {
View menuView = mParent.getLayoutInflater().inflate(
R.layout.my_simple_popupwindow_menu, null);
menuView.findViewById(R.id.rlyt_menu).setOnClickListener(this);
menuView.findViewById(R.id.rlyt_code).setOnClickListener(this);
menuView.findViewById(R.id.rlyt_his).setOnClickListener(this);
return menuView;
} @Override
public void onClick(View view) {
switch (view.getId()) {
// 点击空白处
case R.id.rlyt_menu:
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
break; // 扫一扫
case R.id.rlyt_code:
break; // 历史记录
case R.id.rlyt_his:
break;
}
} /**
* 返回View
*
* @return
*/
public PopupWindow getMenu() {
return popupWindow;
}
}

这段代码中的popupWindow.setAnimationStyle(R.style.PopAnimation);这行代码须要特别注意,作用于对话框的弹出效果,怎样使用可关注的我的博文:http://blog.csdn.net/stoppig/article/details/21481057。使用原理是差点儿相同的。

主activity代码:

package com.example.popwindow;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout; public class MainActivity extends Activity { private MySimplePopMenu popMenu;// 弹出选择框
private PopupWindow mPopupWindow; // 弹出框
private ImageView app_iv_search;
private boolean mIsFirst = true; // 是否第一次初始化弹出框
private Context mContext;
private RelativeLayout mLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
initListener();
this.mContext = this;
} private void findViews() {
app_iv_search = (ImageView) findViewById(R.id.app_iv_search);
mLayout = (RelativeLayout) findViewById(R.id.rv_main);
} private void initListener() {
app_iv_search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mIsFirst) {
mIsFirst = false;
int height = mLayout.getHeight() - v.getHeight();
popMenu = new MySimplePopMenu(MainActivity.this, height);
mPopupWindow = popMenu.getMenu();
} if (mPopupWindow == null) {
return;
} if (mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
return;
} mPopupWindow.showAtLocation(mLayout, Gravity.BOTTOM, 0, 0);
}
});
} /**
* 获取屏幕高度
*
* @param context
* @return
*/
private int getScreenHeight(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getHeight();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

ps:这段代码mIsFirst这个属性是特别注意的。这个属性控制着对话框弹出的次数,假设没有进行控制。每点一次就会弹出一次。

主要代码就这样差点儿相同了。资源地址请点击:下载资源

版权声明:本文博主原创文章,博客,未经同意不得转载。

Android于popWindow写弹出菜单的更多相关文章

  1. Android 使用PopupWindow实现弹出菜单

    在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...

  2. 【Android】5.6 弹出菜单(PopUp Menus)

    分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 功能描述:用户单击按钮弹出菜单.当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失:如果 ...

  3. 【Android】创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  4. 【转】android创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  5. Android开发技巧——使用PopupWindow实现弹出菜单

    在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...

  6. android 单选、多选弹出菜单

    菜单单选窗口: import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInte ...

  7. Android ListView两种长按弹出菜单方式

    转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...

  8. 【转】 教你如何创建类似QQ的android弹出菜单

    原文地址:http://www.apkbus.com/android-18034-1-1.html 大家可能看到android的自带的系统菜单比较难看,如图: 2011-12-4 23:13 上传 下 ...

  9. Android ListView 长按列表弹出菜单

    Android ListView 长按列表弹出菜单 设置长按菜单 listView.setOnCreateContextMenuListener(new View.OnCreateContextMen ...

随机推荐

  1. HDU 1251统计难题 字典树

    字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...

  2. AE加载不同数据的方法(GeoDatabase空间数据管理)

    原文 AE加载不同数据的方法(GeoDatabase空间数据管理) GeoDatabase 先看一下GeoDatabase核心结构模型图: 1  工作空间工厂WorkspaceFactory对象 Wo ...

  3. IOS计算两点之间的距离

    //广州经纬度 CLLocationCoordinate2D guangZhouLocation; guangZhouLocation.latitude = 23.20; guangZhouLocat ...

  4. Java随机验证吗

    <span style="font-size:18px;">package com.java.process.jsp; import java.awt.Color; i ...

  5. Android系统开发(1)——GCC编译器的编译和安装过程

    GCC编译器介绍 GCC编译器(GNG C Compiler)是GNU项目中符合ANSI C标准的编译系统,能够编译C  C++  Object C等语言编写的程序,同时GCC也是一个交叉编译器,特别 ...

  6. Lucene学习总结之一:全文检索的基本原理 2014-06-25 14:11 666人阅读 评论(0) 收藏

    一.总论 根据http://lucene.apache.org/java/docs/index.html 定义: Lucene 是一个高效的,基于Java 的全文检索库. 所以在了解Lucene之前要 ...

  7. [E2E] Robot Framework introduction

    We will use demo project as an example, go though QuickStart repo. Install: First you should have py ...

  8. nyoj 949哈利波特(细节题)

    哈利波特 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Harry 新学了三种魔法.他能够用第一种魔法把 a 克的沙子变成 b 克金属,能够用另外一种魔法把 c 克 ...

  9. php函数实现显示几秒前,几分钟前,几天前等方法(网络上什么都有)

    php函数实现显示几秒前,几分钟前,几天前等方法(网络上什么都有) 一.总结 网络上面什么函数都有 二.php函数实现显示几秒前,几分钟前,几天前等方法 现在很多网站的时间显示都很人性化,不再是单纯的 ...

  10. IE浏览器下css hack

    \9    :所有IE浏览器都支持 _和-  :仅IE6支持 *     :IE6.IE7支持 \0    :IE8.IE9支持 \9\0  :IE8部分支持.IE9支持 \0\9  :IE8.IE9 ...