popupwindows
<1>.能够点击popupwindow 内部空间,外部触摸消失,外部点击事件。
package myapplication.com.myapplication; import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements PopupWindow.OnDismissListener { TextView textView_type;
int state = 0;
PopupWindow pop;
View layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_type = (TextView) findViewById(R.id.click);
popshop();
// popupwindows 外部的点击事件
TextView textView = (TextView) findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "外部点击", Toast.LENGTH_SHORT).show();
}
}); } private void popshop() {
// textView_type= (TextView) view.findViewById(R.id.type_textview);
// mClassifyPopupWindow.showAtLocation(getActivity().
// findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); textView_type.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (state == 1 && pop.isShowing()) {
state = 0;
pop.dismiss();
} else {
/*** 弹出自定义的菜单 ***/
int height;
Display defaultDisplay = getWindowManager().getDefaultDisplay();
height = defaultDisplay.getHeight() / 10 * 7;
layout = View.inflate(MainActivity.this, R.layout.popwindow_layout, null);
//popweindows 里面可点击
TextView pop_textview1 = (TextView) layout.findViewById(R.id.pop_textview1);
pop_textview1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pop.dismiss();
}
});
pop = new PopupWindow(layout, defaultDisplay.getWidth(),
height);
// ColorDrawable cd = new ColorDrawable(-0000);
// pop.setBackgroundDrawable(cd);
// pop.showAsDropDown(v); pop.update();
pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
pop.setTouchable(true); // 设置popupwindow可点击
pop.setOutsideTouchable(true); // 设置popupwindow外部可点击
// pop.setFocusable(true); // 获取焦点 这时外部不可点击触摸
/* 设置popupwindow的位置 */
pop.showAtLocation(layout,
(Gravity.BOTTOM - textView_type.getHeight())
| Gravity.LEFT, 0, 2 * textView_type.getHeight());
state = 1;
pop.setTouchInterceptor(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
pop.dismiss();
return true;
}
return false;
} }); }
}
}); } @Override
public void onDismiss() {
pop.dismiss(); } @Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub if (pop != null && pop.isShowing()) { pop.dismiss(); pop = null; } return super.onTouchEvent(event); } }
<2>.Mainacitivty.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="myapplication.com.myapplication.MainActivity"> <TextView
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="点击弹出PopWindows" /> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="pop弹出时仍能点击" />
</LinearLayout>
<3>. popwindow_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff5555" android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="popwindows内部点击事件"
android:id="@+id/pop_textview1"
android:textSize="22dp"
android:gravity="center_horizontal"/>
<View
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:background="#000000" /> <myapplication.com.myapplication.XCFlowLayout
android:id="@+id/flowlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" > </myapplication.com.myapplication.XCFlowLayout> </LinearLayout>
<4> 自定义菜单栏
pop的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6e6e6e"
android:orientation="vertical"> <TextView
android:id="@+id/textView1"
android:layout_width="140dp"
android:layout_height="40dp"
android:drawableLeft="@mipmap/yun"
android:gravity="center_horizontal|center_vertical"
android:text="菜单"
android:textColor="#000000"
android:textSize="17sp" /> <TextView
android:id="@+id/textView2"
android:layout_width="140dp"
android:layout_height="40dp"
android:drawableLeft="@mipmap/yun"
android:gravity="center_horizontal|center_vertical"
android:text="收藏"
android:textColor="#000000"
android:textSize="17sp" /> <TextView
android:id="@+id/textView3"
android:layout_width="140dp"
android:layout_height="40dp"
android:drawableLeft="@mipmap/yun"
android:gravity="center_horizontal|center_vertical"
android:text="爱好"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>
方法实现:
/**
*
* @param view view点击事件,弹出popupwindow,view传入作为参数
*/
private void showPopupWindow_dingbu(View view) { /**
* 一个自定义的布局,作为显示的内容
*/ View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_layout, null); final PopupWindow popupWindow = new PopupWindow(contentView,
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
WindowManager manager=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
int xpos=manager.getDefaultDisplay().getWidth()/2-popupWindow.getWidth()/2; TextView textView3 = (TextView) contentView.findViewById(R.id.textView1);
/**
* 设置按钮的点击事件
*/
textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"ok",Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
}); popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
}); popupWindow.showAtLocation(view, Gravity.TOP|Gravity.RIGHT, 0, 140); //右下
popupWindow.showAsDropDown(view);
}
popupwindows的更多相关文章
- Android PopupWindows
今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,能够使用随意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的. 以下是一个实例 xml ...
- PopupWindows 在2.3.3下报java.lang.NullPointerException
03-05 01:20:56.040: E/AndroidRuntime(1396): java.lang.NullPointerException 03-05 01:20:56.040: E/And ...
- Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录
一.问题描述 记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!! 在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout ...
- Android PopupWindows使用
源码测试示例: package com.example.popupwindown; import android.os.Bundle; import android.app.Activity; imp ...
- 自定义PopupWindow
PopupWindow,一个弹出窗口控件,可以用来显示任意View,而且会浮动在当前activity的顶部 自定义PopupWindow. 1.extends PopupWindow 2.构造方法中可 ...
- Dialog详解(包括进度条、PopupWindow、自定义view、自定义样式的对话框)
Dialog详解(包括进度条.PopupWindow.自定义view.自定义样式的对话框) Android中提供了多种对话框,在实际应用中我们可能会需要修改这些已有的对话框.本实例就是从实际出发, ...
- Android高手速成--第一部分 个性化控件(View)
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- 据说年薪30万的Android程序员必须知道的帖子
Android中国开发精英 目前包括: Android开源项目第一篇--个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.G ...
- Android开源项目分类汇总
目前包括: Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...
随机推荐
- 杭电 2095 find your present (2)【位运算 异或】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2095 解题思路:因为只有我们要求的那个数出现的次数为奇数,所以可以用位运算来做,两次异或同一个数最后结 ...
- 团体程序设计天梯赛-练习集-L1-028. 判断素数
L1-028. 判断素数 本题的目标很简单,就是判断一个给定的正整数是否素数. 输入格式: 输入在第一行给出一个正整数N(<=10),随后N行,每行给出一个小于231的需要判断的正整数. 输出格 ...
- css3媒介查询使用规则小结
响应式布局可以为不同终端的用户提供更加舒适的界面和更好的用户体验,而且随着目前大屏幕移动设备的普及,用大势所趋来形容也不为过. 用一句话来说:使用同一套Html代码来适配不同设备和满足不同场景不同用户 ...
- 洛谷P1486 [NOI2004]郁闷的出纳员
Code: #include<cstdio> #include<algorithm> using namespace std; struct Node{ int s,val,t ...
- js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得
js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得
- ZooKeeper伪集群的搭建(Windows)
首先下载 zookeeper 地址:https://www.apache.org/dyn/closer.cgi/zookeeper/ 1.下载完成解压后修改文件夹名字为zookeeper1,然后删除c ...
- MySQL py模块的链接Navicat可视化工具
数据库可视化工具Navicat 1 基本操作: 1 库 表 字段 记录(增删改查) 2 添加主建,添加自增. 3 添加外键,外键的链接 4 模型建表,模型添加外键.(逆向数据库到模型,运行SQL文件 ...
- Win 10安装mysql以及常见问题总结
一.mysql免安装版本配置1.从官网下载安装包,解压后,在电脑属性环境变量的path中配置bin的路径 2.配置my.ini [mysql] # 设置mysql客户端默认字符集 default-ch ...
- js中数组常用方法
1.Array.push() 此方法是在数组的后面添加新加元素,此方法改变了数组的长度: var aa=[1,2,3]; var bb=aa.push(4,5); console.log(bb) ...
- 记一次生产主机中挖矿病毒"kintegrityds"处理过程!
[记一次生产挖矿病毒处理过程]: 可能性:webaap用户密码泄露.Jenkins/redis弱口令等. 1.监控到生产主机一直load告警 2.进服务器 top查看进程,发现挖矿病毒进程,此进程持续 ...