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));
                tv.setText("I‘m a pop -----------------------------!");
                tv.setTextColor(Color.WHITE);
                layout.addView(tv);

                popupWindow = new PopupWindow(layout,120,120);

                popupWindow.setFocusable(true);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());

                int[] location = new int[2];
                v.getLocationOnScreen(location);

                popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
        }

在控件的其他方向上显示只需修改最后一行代码即可,如:

2、下方:

popupWindow.showAsDropDown(v);

在下方,其它方法

int[] location = new int[2];
v.getLocationOnScreen(location);popupWindow.showAtLocation(v, Gravity.TOP, 0,location[1]+v.getHeight());

3、左边:

popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);

4、右边:

popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);

android popupwindow 位置显示

时间: 2024-08-08 13:03:01

android popupwindow 位置显示的相关文章

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

android判断位置显示返回顶部按钮最精简代码

listView.setOnScrollListener(new OnScrollListener() { @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { updateBackTopButtonState(); }}); private void updateBackTopButtonState() { if (ad

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:bac

Android PopupWindow显示位置设置

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

【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻菜单

前一篇文章中有用到 PopupWindow 来实现弹窗的功能.简单介绍以下吧. 官方文档是这样解释的:这就是一个弹出窗口,可以用来显示一个任意视图.出现的弹出窗口是一个浮动容器的当前活动. 1.首先来个简单的栗子,效果如下: 只有两个布局文件,一个是弹窗布局(只有一张图片),一个是主界面布局(只有一个按钮). 然后在主界面代码中实例 PopupWindow ,指定弹出的界面,在按钮点击事件中显示或隐藏弹窗就可以了,代码如下: package com.yanis.demo; import andr

Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar

package cc.testprogressdialog; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.P

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:PopupWindow的使用场景和注意事项

1.PopupWindow的特点 借用Google官方的说法: "A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity." 也就是说.popupwindow是activity上方的一个悬浮容器.它能够显示随意的视图View,非常霸气的样子. 以下看一

Android popupwindow以及windowManager总结——实现悬浮效果

Android有三类窗口 应用程序窗口 (Application Window): 包括所有应用程序自己创建的窗口,以及在应用起来之前系统负责显示的窗口. 子窗口(Sub Window):比如应用自定义的对话框,或者输入法窗口,子窗口必须依附于某个应用窗口(设置相同的token). 系 统窗口(System Window): 系统设计的,不依附于任何应用的窗口,比如说,状态栏(Status Bar), 导航栏(Navigation Bar), 壁纸(Wallpaper), 来电显示窗口(Phon