12 PopupWindow
PopupWindow创建方式
- PopupWindow pop = new PopupWindow()
- PopupWindow pop = new PopupWindow(上下文, 填充宽, 填充高);
PopupWindow pop = new PopupWindow()案例1:
java代码:
package com.fmy.popwindowdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
//获取布局填充器
LayoutInflater inflater = LayoutInflater.from(this);
//把布局填充一个view
View converView = inflater.inflate(R.layout.layout, null);
//创建一个pop
PopupWindow pop = new PopupWindow();
//吧view放入
pop.setContentView(converView);
//设置pop的宽
pop.setWidth(LayoutParams.MATCH_PARENT);
//设置pop的高
pop.setHeight(LayoutParams.MATCH_PARENT);
//找到一个view 为了设置pop弹出的位置
View tv = findViewById(R.id.tv);
//弹出到tv下方
pop.showAsDropDown(tv);
}
}
填充成view 的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffff00"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不好" />
</LinearLayout>
main布局文件
<RelativeLayout 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: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="com.fmy.popwindowdemo.MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="嘿嘿" />
</RelativeLayout>
效果图
解释效果图:
pop.setWidth(LayoutParams.MATCH_PARENT);
//设置pop的高
pop.setHeight(LayoutParams.MATCH_PARENT);
pop.showAsDropDown(tv);
必须要在tv下方 由填充父元素所以导致生成上述界面
PopupWindow pop = new PopupWindow()案例2:
java代码
package com.fmy.popwindowdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
//获取布局填充器
LayoutInflater inflater = LayoutInflater.from(this);
//把布局填充一个view
View converView = inflater.inflate(R.layout.layout, null);
//创建一个pop
PopupWindow pop = new PopupWindow();
//吧view放入
pop.setContentView(converView);
//设置pop的宽
pop.setWidth(LayoutParams.WRAP_CONTENT);
//设置pop的高
pop.setHeight(LayoutParams.WRAP_CONTENT);
//找到一个view 为了设置pop弹出的位置
View tv = findViewById(R.id.tv);
//弹出到tv下方 并且相对于此位置的偏移量
pop.showAsDropDown(tv,100, 50);
}
}
其他代码 和上一个例子一样
效果图
效果图解释
//设置pop的宽
pop.setWidth(LayoutParams.WRAP_CONTENT);
//设置pop的高
pop.setHeight(LayoutParams.WRAP_CONTENT);
//找到一个view 为了设置pop弹出的位置
View tv = findViewById(R.id.tv);
//弹出到tv下方 并且相对于此位置的偏移量
pop.showAsDropDown(tv,100, 50);
在tv控件下方然后相对于x便宜100 还有y便宜50 并且由于pop为包裹内容故有此效果
PopupWindow pop = new PopupWindow(上下文, 填充宽, 填充高);并相对于父布局位置案例1:
java代码:
package com.fmy.popwindowdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
//获取布局填充器
LayoutInflater inflater = LayoutInflater.from(this);
//把布局填充一个view
View converView = inflater.inflate(R.layout.layout, null);
//创建一个pop
PopupWindow pop = new PopupWindow(converView,android.app.ActionBar.LayoutParams.WRAP_CONTENT,android.app.ActionBar.LayoutParams.WRAP_CONTENT);
//找到一个view 为了设置pop弹出的位置
View tv = inflater.inflate(R.layout.activity_main, null);
pop.showAtLocation(tv, Gravity.RIGHT,0,50);
}
}
效果图:
效果图解释:
View tv = inflater.inflate(R.layout.activity_main, null);
pop.showAtLocation(tv, Gravity.RIGHT,0,50);
由于相对于父布局显示 又设置了Gravity.RIGHT所以pop在右边显示 然后在Y轴偏移50导致了最后的效果
12 PopupWindow的更多相关文章
- python 各模块
01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- 在mybatis中写sql语句的一些体会
本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...
- PopupWindow 使用
昨天马失前蹄,为了做一个小键盘,耽误了两个小时,记录一下心路历程 1.关于需求与选择 需求: 点击一个按钮,弹出一个小键盘(类似于输入法键盘) 选择: (1)方案一:KeyboardView 这是百度 ...
- android模仿58筛选下拉框(PopupWindow实现)
前言:前几天用58同城APP找房子的时候,看到筛选下拉框蛮不错的,然后也有很多朋友需要实现这个功能,于是从网上下载了一个demo,在他的基础上进行修改,花了几个小时对他的代码进行修改,重构,封装.把一 ...
- Android中的PopupWindow
1.功能 PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的,可以设置显示位置. 2.需求 弹出软键盘,实现键盘功能从而 ...
- 使用PopupWindow
PopupWindow可以用来实现弹出任意位置的菜单,比Context Menu和Option Menu灵活性更高.Android中弹出一个PopupWindow基本有两个方法: 1 2 //Disp ...
- Android popupwindow 弹出的位置问题
在Android开发中,需要用到PopupWindow这个类.在初始化完成,显示之前,都需要获得这个对象的width,height去计算popupWindow弹出的位置. 这个时候会发现取得的widt ...
- popupwindow 模拟新浪、腾讯title弹框效果
.jpg外部引用 原始文档 MainActivity.java外部引用 原始文档 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
随机推荐
- [NOI2005]寿司晚宴
题目描述 为了庆祝NOI的成功开幕,主办方为大家准备了一场寿司晚宴.小G和小W作为参加NOI的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了n−1种不同的寿司,编号1,2,3,⋯,n-1 ...
- bzoj 3451 Normal
Description 某天WJMZBMR学习了一个神奇的算法:树的点分治! 这个算法的核心是这样的: 消耗时间=0 Solve(树 a) 消耗时间 += a 的 大小 如果 a 中 只有 1 个点 ...
- 2017ACM/ICPC广西邀请赛-重现赛 1004.Covering
Problem Description Bob's school has a big playground, boys and girls always play games here after s ...
- Python中dict的功能介绍
Dict的功能介绍 1. 字典的两种函数(方法) 1. 字典的内置函数 包含关系 格式:x.__contains__(key)等同于key in x 例如:dic = {'ab':23,'cd':34 ...
- mybatis什么时候用resulttype 什么时候用resultmap
如果你搜索只是返回一个值,比如说String ,或者是int,那你直接用resultType就行了. 但是你如果是返回一个复杂的对象,就必须定义好这个对象的resultMap的result map. ...
- 三种方法,刷新 Android 的 MediaStore!让你保存的图片立即出现在相册里!
公众号原标题:测试:"系统相册里怎么看不到我刚保存的图片,是我操作不对吗?" 一.序 Hi,大家好,我是承香墨影! App 内,创建一个文件并保存文件到本地的需求,是很常见的 I/ ...
- Java 读取 json文件
public ResponseBean getAreas() { String path = getClass().getClassLoader().getResource("area.js ...
- 位运算n & (n-1)的妙用
本文转自:http://blog.csdn.net/zheng0518/article/details/8882394 按位与的知识 n&(n-1)作用:将n的二进制表示中的最低位为1的改为0 ...
- HybridAPP开发框架Ionic+AngularJS+Cordova搭建
Ionic Ionic是一个新的.可以使用HTML5构建混合移动应用的用户界面框架,它自称为是"本地与HTML5的结合".该框架提供了很多基本的移动用户界面范例,例如像列表(lis ...
- Python小代码_14_交换 2 个变量的 3 种方式
a = 4 b = 5 #第一种 c = a a = b b = c print(a, b) #输出结果 #5 4 #第二种 a = a + b b = a - b a = a - b print(a ...