watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb3l1YW41MTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

<?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="#DA4F30"
android:cacheColorHint="#000000"
android:orientation="vertical" > <ListView
android:id="@+id/lv_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:scrollbars="none"
android:overScrollMode="never">
</ListView> </LinearLayout>
package com.example.showpop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow; public class MainActivity extends Activity {
private LinearLayout layout;
private ListView listView;
private PopupWindow popupWindow;
private String title[] = { "1", "2", "3", "4", "5" };
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
imageView=(ImageView) findViewById(R.id.imageview_above_more);
imageView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
showPopupWindow(imageView);
}
});
}
public void showPopupWindow(View parent) {
//载入布局
layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
R.layout.dialog, null);
//找到布局的控件
listView = (ListView) layout.findViewById(R.id.lv_dialog);
//设置适配器
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
R.layout.text, R.id.tv_text, title));
// 实例化popupWindow
popupWindow = new PopupWindow(layout, 300,500);
//控制键盘能否够获得焦点
popupWindow.setFocusable(true);
//设置popupWindow弹出窗口的背景
popupWindow.setBackgroundDrawable(new BitmapDrawable(null,""));
WindowManager manager=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
@SuppressWarnings("deprecation")
//获取xoff
int xpos=manager.getDefaultDisplay().getWidth()/2-popupWindow.getWidth()/2;
//xoff,yoff基于anchor的左下角进行偏移。 popupWindow.showAsDropDown(parent,xpos, 0);
//监听
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<? > arg0, View arg1, int arg2,
long arg3) {
//关闭popupWindow
popupWindow.dismiss();
popupWindow = null;
}
});
}
}

源代码下载地址

Android PopupWindow显示位置和显示大小的更多相关文章

  1. 自定义Toast的显示位置和显示内容

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. Winform WPF 窗体显示位置

    WinForm 窗体显示位置 窗体显示的位置首先由窗体的StartPosition决定,FormStartPosition这个枚举值由如下几种情况 // 摘要: // 窗体的位置由 System.Wi ...

  3. Android PopupWindow显示位置设置

    当点击某个按钮并弹出PopupWindow时,PopupWindow左下角默认与按钮对齐,但是如果PopupWindow是下图的那样,会发 生错位的情况,尤其是不同尺寸的平板上,那错位错的不是一般的不 ...

  4. Android popupwindow在低版本手机上无法显示

    popupwindow偶尔的显示失效(在低版本Android系统的手机上,测试机6.0)实在是坑害了不少人,害,而且坑了for a long time.本小白就是其中一个受害者. 百度了N久N多还是没 ...

  5. bug -- android 7.0 popwindow显示位置异常情况解决

    android 7.0 popwindow显示位置异常,在android7.1官方进行解决了,但是还是要多7.0的bug进行解决,我的解决方案里面通过重写popwindow进行适配: import a ...

  6. (转载) popupWindow 指定位置上的显示

    popupWindow 指定位置上的显示 标签: androidpopupWindowpopupWindow具体位置放置 2014-07-09 16:23 1114人阅读 评论(0) 收藏 举报  分 ...

  7. (android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图

    (android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图 http://www.cnblogs.com/macroxu-1982/archive/2011/09/13 ...

  8. Android PopupWindow怎么合理控制弹出位置(showAtLocation)

    说到PopupWindow,应该都会有种熟悉的感觉,使用起来也很简单 // 一个自定义的布局,作为显示的内容 Context context = null; // 真实环境中要赋值 int layou ...

  9. Android窗口管理服务WindowManagerService显示窗口动画的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8611754 在前一文中,我们分析了Activi ...

随机推荐

  1. linux技术框架

    编程语言 一般使用c或者c++ linux使用 鸟哥私房菜 工具使用 代码编辑source insight,代码编译gcc,代码调试gdb,代码编译组织makefile,命令执行shell,文本编辑n ...

  2. Dream

    即使下着雨,天空依旧明亮,因为远方有我的梦想.   ——forever97

  3. Android 4.4及以上系统下应用的状态栏颜色渐变效果的实现

    上一篇转载的博文里讲到了怎么开启状态栏透明的效果,不过如果在有ActionBar的情况下,会出现状态栏透明而ActionBar横亘在状态栏和内容之间的丑陋情况,如下图: 通过百度之后,发现了GitHu ...

  4. android 安全未来怎么走

  5. centos 5.8 x64Jetty的安装以及项目部署配置

    链接地址:http://blog.csdn.net/shuixin536/article/details/9049821 安装环境 centos 5.8 x64 安装前须知 首先在安装Jetty之前要 ...

  6. Android各种效果集合

    QQ侧滑风格:http://www.cnblogs.com/lichenwei/p/4111252.html,通过继承HorizontalScrollView类来实现的.

  7. block, inline和inline-block的区别

    display:block元素的特点是:总是在新行上开始:高度,行高以及顶和底边距都可控制:宽度缺省是它的容器的100%,除非设定一个宽度<div>, <p>, <h1& ...

  8. 使用PHP搭建自己的MVC框架

    一.什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...

  9. poj 1604 Just the Facts

    /** 大意: 求n! 结果 从左到右 第一个非零数 跟 1150 差不多.. **/ #include <iostream> #include <cstdio> using ...

  10. (Problem 29)Distinct powers

    Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...