实现将一个View显示在某一位置,而且是浮于当前窗口

首先要有一个要显示的view的布局,可以是任意View,包括ViewGroup

 <?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="fill_parent"
android:background="#d3d3d3"
android:gravity="center_horizontal"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/textView"
android:text="文本内容"
/>
</LinearLayout>

然后主界面布局文件

 <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"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!-- 区域 -->
<Button
android:layout_width="0dp"
android:layout_weight=""
android:gravity="center"
android:background="#0000"
android:layout_height="wrap_content"
android:text="区域"
android:drawableRight="@drawable/title_bar_arrow_nor"
android:id="@+id/btn_1"
/>
<!-- 类别 -->
<Button
android:background="#0000"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="类别"
android:drawableRight="@drawable/title_bar_arrow_nor"
android:id="@+id/btn_2"
/> <!-- 价格 -->
<Button
android:background="#0000"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="价格"
android:drawableRight="@drawable/title_bar_arrow_nor"
android:id="@+id/btn_3"
/> <!-- 更多 -->
<Button
android:background="#0000"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="更多"
android:drawableRight="@drawable/title_bar_arrow_nor"
android:id="@+id/btn_4"
/> </LinearLayout> </RelativeLayout>

主activity

 package com.example.aaa;

 import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private PopupWindow pop; private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4; private TextView text; View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
initPopWindow(); } private void initView() {
// TODO Auto-generated method stub
btn1 = (Button) findViewById(R.id.btn_1);
btn2 = (Button) findViewById(R.id.btn_2);
btn3 = (Button) findViewById(R.id.btn_3);
btn4 = (Button) findViewById(R.id.btn_4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
} private void initPopWindow() {
// TODO Auto-generated method stub
//根据layout创建弹出界面
view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
text = (TextView) view.findViewById(R.id.textView); pop.setOutsideTouchable(true);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pop.dismiss();
}
}); } @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_1:
text.setText("区域");
if(pop.isShowing())
pop.dismiss();
else
pop.showAsDropDown(v);
break;
case R.id.btn_2:
text.setText("类别");
if(pop.isShowing())
pop.dismiss();
else
pop.showAsDropDown(v);
break;
case R.id.btn_3:
text.setText("价格");
if(pop.isShowing())
pop.dismiss();
else
pop.showAsDropDown(v);
break;
case R.id.btn_4:
text.setText("更多");
if(pop.isShowing())
pop.dismiss();
else
pop.showAsDropDown(v);
break; default:
break;
}
} }

出现位置的几个方法

showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

效果图:

浅谈PopupWindow弹出菜单的更多相关文章

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

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

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

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

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

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

  4. 如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题

    如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题 如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题 在android中有时候可能要实现一个底部弹 ...

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

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

  6. 用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)

         用PopupWindow实现弹出菜单是一个比较好的方式.当然我们还有一个类PopupMenu也能实现弹出菜单,但那个太过于局限了,所以不是很推荐. 这个实例的效果是这样的:点击按钮后,一个菜 ...

  7. PopupWindow(2)简单示例-自定义弹出菜单

    本示例,用 popupWindow 自定义弹出菜单 public class CustomActionProvider extends ActionProvider implements OnMenu ...

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

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

  9. dragView 屏幕拖拽并且弹出菜单的控件

    dragView 因项目新需求需要添加一个屏幕拖拽按钮可以弹出菜单的控件,因为不是我做的闲来无事写一个demo吧 可能存在一些小bug(毕竟就写了几个小时)兄弟姐妹们理解思路就行 具体的可以自己调试一 ...

随机推荐

  1. 【PRML读书笔记-Chapter1-Introduction】1.4 The Curse of Dimensionality

    维数灾难 给定如下分类问题: 其中x6和x7表示横轴和竖轴(即两个measurements),怎么分? 方法一(simple): 把整个图分成:16个格,当给定一个新的点的时候,就数他所在的格子中,哪 ...

  2. 字体文件放入CDN服务器中,跨域问题(IIS版)

    Font from origin 'http:/XXXX' has been blocked from loading by Cross-Origin Resource Sharing policy: ...

  3. 如何将ToolBar 样式设置Title文字水平居中

    以下是我的activity.xml的代码,线性布局.<android.support.v7.widget.Toolbar         android:id="@+id/toolba ...

  4. UITextField-secureTextEntry

    1.UITextFiled的密文输入   secureTextEntry  安全文本输入  secure:安全  Entry:入口

  5. (非妙味3):浏览器window事件:及浏览各种尺寸介绍

    (触发)window.onload;  window.onscroll;   window.onresize; (兼容)网页可视区尺寸.网页全文尺寸.滚动距离 (实例)广告块高度动态居中.回到顶部   ...

  6. Java 中文字符判断 中文标点符号判断

    Java Character 实现Unicode字符集介绍  CJK中文字符和中文标点判断 主要内容: 1. Java Character类介绍: 2. Unicode 简介及 UnicodeBloc ...

  7. 1215 spring 3 项目更新

    列志华 (组长) http://www.cnblogs.com/liezhihua/ 团队guihub https://github.com/LWHTF/OrderingFood 黄柏堂 http:/ ...

  8. ADO.NET 基础

    *程序要和数据库交互要通过ADO.NET进行,通过ADO.NET就能在程序中执行SQL了,ADO.NET中提供了对各种不同数据库的统一操作接口. 1.连接SQLServer 连接字符串,程序通过链接字 ...

  9. eclipse中改变默认的workspace的方法及说明

    eclipse中改变默然的workspace的方法可以有: 1.在创建project的时候,手动选择使用新的workspace,如创建一个web project,在向导中的Location选项,取消使 ...

  10. 从C#中通过Windows窗体添加信息到数据库 (添加学生信息)

    如上图所示界面,当我们点击保存按钮时将会将表格中的数据保存到数据库中去,与数据库进行一个交互 第一步我们就是要获取到表格中的数据 string pwd = textpwd.Text; //获得第一次输 ...