Android于popWindow写弹出菜单
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写弹出菜单的更多相关文章
- Android 使用PopupWindow实现弹出菜单
在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...
- 【Android】5.6 弹出菜单(PopUp Menus)
分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 功能描述:用户单击按钮弹出菜单.当用户选择一个菜单项,会触发MenuItemClick事件并让弹出的菜单消失:如果 ...
- 【Android】创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- 【转】android创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- Android开发技巧——使用PopupWindow实现弹出菜单
在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...
- android 单选、多选弹出菜单
菜单单选窗口: import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInte ...
- Android ListView两种长按弹出菜单方式
转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...
- 【转】 教你如何创建类似QQ的android弹出菜单
原文地址:http://www.apkbus.com/android-18034-1-1.html 大家可能看到android的自带的系统菜单比较难看,如图: 2011-12-4 23:13 上传 下 ...
- Android ListView 长按列表弹出菜单
Android ListView 长按列表弹出菜单 设置长按菜单 listView.setOnCreateContextMenuListener(new View.OnCreateContextMen ...
随机推荐
- 10.8 android输入系统_实战_使用GlobalKey一键启动程序
11. 实战_使用GlobalKey一键启动程序参考文章:Android 两种注册(动态注册和静态注册).发送广播的区别http://www.jianshu.com/p/ea5e233d9f43 [A ...
- Java学习很好的笔记
http://www.cnblogs.com/vamei/archive/2013/03/31/2991531.html
- ORA-00119: invalid specification for system parameter LOCAL_LISTENER;
错误提示内容及上下文环境: SQL> grant sysdba to weng;grant sysdba to weng*第 1 行出现错误:ORA-01034: ORACLE not avai ...
- 怎样自己构建一个小型的Zoomeye----从技术细节探讨到实现
转载请注明出处:viewmode=list">http://blog.csdn.net/u011721501?viewmode=list 0.概述 Zoomeye是个网络空间的搜 ...
- ZOJ-1649 Rescue BFS (HDU 1242)
看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...
- 观CSDN站点小Bug有感
今天早上在浏览博客的时候偶然发现CSDN博客的数据出现了异常,我也是头一次看到这么明显的Bug.详细什么表现呢?先来看个截图.例如以下: 常常看CSDN博客的人 ...
- php实现反转链表(链表题一定记得画图)(指向链表节点的指针本质就是一个记录地址的变量)($p->next表示的是取p节点的next域里面的数值,next只是p的一个属性)
php实现反转链表(链表题一定记得画图)(指向链表节点的指针本质就是一个记录地址的变量)($p->next表示的是取p节点的next域里面的数值,next只是p的一个属性) 一.总结 链表反转两 ...
- angular的学习参考材料
原文地址:https://www.jianshu.com/p/b9db7bb3d4ec 目的 其实写这篇文章的主要目的是为了提供给那些刚刚入门angular或者有意学习angular的读者准备的. 我 ...
- github-vimium-compile-crx
chrome-62.x版本上安装vimium 1.61.1 https://blog.csdn.net/ZHUJIANWEILI4/article/details/78385346
- Hadoop1.2.1伪分布模式安装指南 分类: A1_HADOOP 2014-08-17 10:52 1346人阅读 评论(0) 收藏
一.前置条件 1.操作系统准备 (1)Linux可以用作开发平台及产品平台. (2)win32只可用作开发平台,且需要cygwin的支持. 2.安装jdk 1.6或以上 3.安装ssh,并配置免密码登 ...