<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的更多相关文章

  1. Android PopupWindows

    今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,能够使用随意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的. 以下是一个实例 xml ...

  2. 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 ...

  3. Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录

    一.问题描述     记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!!     在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout ...

  4. Android PopupWindows使用

    源码测试示例: package com.example.popupwindown; import android.os.Bundle; import android.app.Activity; imp ...

  5. 自定义PopupWindow

    PopupWindow,一个弹出窗口控件,可以用来显示任意View,而且会浮动在当前activity的顶部 自定义PopupWindow. 1.extends PopupWindow 2.构造方法中可 ...

  6. Dialog详解(包括进度条、PopupWindow、自定义view、自定义样式的对话框)

    Dialog详解(包括进度条.PopupWindow.自定义view.自定义样式的对话框)   Android中提供了多种对话框,在实际应用中我们可能会需要修改这些已有的对话框.本实例就是从实际出发, ...

  7. Android高手速成--第一部分 个性化控件(View)

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  8. 据说年薪30万的Android程序员必须知道的帖子

    Android中国开发精英 目前包括: Android开源项目第一篇--个性化控件(View)篇       包括ListView.ActionBar.Menu.ViewPager.Gallery.G ...

  9. Android开源项目分类汇总

    目前包括: Android开源项目第一篇——个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...

随机推荐

  1. 使用meta实现页面的定时刷新或跳转

    <meta http-equiv="refresh" content="5"> 这个表示当前页面每5秒钟刷一下,刷一下~ <meta http ...

  2. 00-- 关于C++ const 的全面总结

    转:关于C++ const 的全面总结 C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C ...

  3. 03--SQLtie三言两语SQLtie链接(join)

    本文将从连接的理论和语法讲起,结合具体的例子,详细分析 SQL 连接. 之前对数据库的连接操作似懂非懂,大概知道是什么东西,但是面试笔试的时候被虐成渣,讲不清连接到底是什么.吃一堑,长一智.这就是所谓 ...

  4. Java中的自动转换

    特点: 1. 系统自动完成的,不需要程序员手动修改代码 2.将 取值范围小的类型 自动提升为 取值范围大的类型 注意: 整数类型直接写会默认为int  小数类型直接写默认为double 类型的范围大小 ...

  5. Python中生成器,迭代器,以及一些常用的内置函数.

    知识点总结 生成器 生成器的本质就是迭代器. 迭代器:Python中提供的已经写好的工具或者通过数据转化得来的. 生成器:需要我们自己用Python代码构建的 创建生成器的三种方法: 通过生成器函数 ...

  6. Java范式1

    package Xwxx; public class Person { private String name; private int age; public Person() { } public ...

  7. [NOI2005]瑰丽华尔兹_动态规划_单调队列

    Code: #include<cstdio> #include<cstring> #include<deque> #include<algorithm> ...

  8. 【数据分析学习】Pandas思维导图

    点我查看原版

  9. Linux系统下的 /etc/fstab 文件解读

    1 [root@localhost ~]# cat /etc/fstab 2 3 # 4 # /etc/fstab 5 # Created by anaconda on Sat Nov 3 12:03 ...

  10. 常见的dos命令(及抒写方式)

    1.dir:列出当前目录下的主体及文件夹. 2.md:创建目录. 3.rd:删除目录.{注意:rd不能删除非空的文件夹,并且只能用于文件夹的删除} 3.cd :进入指定目录. 4.cd . . :退出 ...