Android PopupWindow显示位置和显示大小

<?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显示位置和显示大小,布布扣,bubuko.com

时间: 2024-08-06 07:51:50

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

Android PopupWindow显示位置设置

当点击某个按钮并弹出PopupWindow时,PopupWindow左下角默认与按钮对齐,但是如果PopupWindow是下图的那样,会发 生错位的情况,尤其是不同尺寸的平板上,那错位错的不是一般的不靠谱,而Android本身只提供了如下几个方法设置PopupWindow显示位置 showAsDropDown(View anchor, int xoff, int yoff) 以anchor的左下角为参照点,定义偏移 showAsDropDown(android.view.View) 以ancho

android popupwindow 位置显示

1.在控件的上方: private void showPopUp(View v) { LinearLayout layout = new LinearLayout(this); layout.setBackgroundColor(Color.GRAY); TextView tv = new TextView(this); tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT

设置popupWindow显示位置以及点击其他位置取消弹出

相对控件位置显示: 上方显示 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private void showPopUp(View v) {         LinearLayout layout = new LinearLayout(this);         layout.setBackgroundColor(Color.GRAY);         TextView tv = new TextView(this);       

自定义Popupwindow并指定显示位置!

直接上代码 activity_popup_window_tooltip_text.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heig

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

(android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图 http://www.cnblogs.com/macroxu-1982/archive/2011/09/13/2174657.html 实现效果: 获取手机gps当前的位置,显示位置对于的google地图. 具体的步骤: 1 Step One  创建包含MapView控件的应用界面 <com.google.android.maps.MapView android:layout_width="fill

使用Dimension类和Point类设置窗体大小和显示位置

import javax.swing.*; import java.awt.*; public class ss1{ public static void main(String[] args){ JFrame f=new JFrame("图形界面"); // Dimension d=new Dimension(); //创建Dimension对象 d.setSize(466,150); //指定组件大小 f.setSize(d); //设置组件大小 f.setBackground(C

android项目中记录ListView滚动停止位置与设置显示位置

在项目中经常使用到listView控件,当想记录滚动停止时的记录,当点击加载新的数据,从记录的位置开始显示的操作怎么实现尼?分为如下步骤 1.记录位置代码 [java] view plaincopy //声明记录停止滚动时候,可见的位置 private int stop_position; [java] view plaincopy @Override public void onScrollStateChanged(AbsListView view, int scrollState) { //

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

说到PopupWindow,应该都会有种熟悉的感觉,使用起来也很简单 // 一个自定义的布局,作为显示的内容 Context context = null; // 真实环境中要赋值 int layoutId = 0; // 布局ID View contentView = LayoutInflater.from(context).inflate(layoutId, null); final PopupWindow popupWindow = new PopupWindow(contentView,

Android项目:输入法软键盘显示/隐藏的监听和控制,InputMethodManager用法研究

在项目开发中,用到编辑框的地方经常涉及到要监听或者控制软键盘的显示/隐藏状态.本以为这是很容易解决的一个小问题,没想到当初碰到这个问题才明白还得花点小心思才能整好.现将针对软键盘的显示/隐藏状态的监听/监控方法做一些总结,以备后用. 一.点击空白处隐藏软键盘 这是具有编辑框焦点的页面对输入法软键盘状态监听的一般需求和解决方法. 首先获得InputMethodManager:        InputMethodManager manager = (InputMethodManager) getS