自定义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_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context="com.artshell.trainingdemos.test.PopupWindowTooltipTextActivity">

    <Button
        android:id="@+id/anchor_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me to see tooltip"
        android:layout_centerInParent="true"/>

</RelativeLayout>

nav_up.xml (drawable)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:fromDegrees="45"
            android:pivotX="-50%"
            android:pivotY="80%"
            android:toDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="#92e4e5"/>
                <stroke
                    android:width="2dp"
                    android:color="#92e4e5"/>
            </shape>
        </rotate>
    </item>
</layer-list>

tooltip_bg.xml (drawable)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#92e4e5"/>
    <stroke
        android:width="2dp"
        android:color="#92e4e5"/>
    <corners android:radius="2dp"/>
</shape>

popup_window_tooltip_layout.xml

<?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:gravity="center"
              android:orientation="vertical">
    <ImageView
        android:id="@+id/tooltip_nav_up"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/nav_up"/>

    <TextView
        android:id="@+id/tooltip_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tooltip_bg"
        android:gravity="center"
        android:padding="10dp"
        android:text="Tooltip using PopupWindow :)"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold"/>

</LinearLayout>

Activity代码:

public class PopupWindowTooltipTextActivity extends ActionBarActivity {
    private View contentView;
    private PopupWindow tipWindow;
    private Button anchor_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window_tooltip_text);
        anchor_view = (Button) findViewById(R.id.anchor_view);

        contentView = getLayoutInflater().inflate(R.layout.popup_window_tooltip_layout, null);

        tipWindow = new PopupWindow(this);

        tipWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    tipWindow.dismiss();
                }
                // 这个地方一定要返回false,你可能会说这里已经消费掉事件了啊应该返回true,参看PopupWindow的私有类PopupViewContainer
                // 弹出的View都是交由这个类来管理的,因为它对onTouchEvent()这个回调方法已经提供了一些默认实现,当你是返回true,事件就在此被拦截
                // 不会再继续派发了,所以onTouchEvent()无法被调用
                return false;
            }
        });

        anchor_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tipWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                tipWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                tipWindow.setOutsideTouchable(true);
                tipWindow.setTouchable(true);
                tipWindow.setFocusable(true);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.easyicon_net);
                // 某些博客说,当没有设置Background的时候,无论是触摸其他空白区还是back键都无法dismiss()掉这个PopupWindow
                // 但我注释掉这段代码能dismiss(),有可能是个Bug,已经得google到修复
                tipWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),bitmap));
                tipWindow.setContentView(contentView);

                // 计算这个PopupWindow的显示位置
                int[] screen_pos = new int[2];
                anchor_view.getLocationInWindow(screen_pos);
                Rect anchor_rect = new Rect(screen_pos[0], screen_pos[1], screen_pos[0] + anchor_view.getWidth(), screen_pos[1] + anchor_view.getHeight());
                contentView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                int contentViewWidth = contentView.getMeasuredWidth();
                int contentViewHeight = contentView.getMeasuredHeight();
                int position_x = anchor_rect.centerX() - (contentViewWidth / 2);
                int position_y = anchor_rect.bottom - (anchor_rect.height() / 2);

                tipWindow.showAtLocation(anchor_view, Gravity.NO_GRAVITY, position_x, position_y);
            }
        });

    }
}

还要注意一点就是:当你的已经dismiss()掉了,想再次显示PopupWindow,那么它的某些属性需要重新设置,不能一处写好多处使用,参看这个dismiss()方法的源码实现。在没有dismiss()之前,你的PopupWindow内容发生变化了,你可以使用它的update()系列方法来更新.......

这里再帖连个参考链接,希望对你有所帮助

http://www.cnblogs.com/mengdd/p/3569127.html 【Android PopupWindow的使用和分析】

http://michaelye1988.iteye.com/blog/1766629 【Popupwindow的使用】

时间: 2024-08-29 15:19:17

自定义Popupwindow并指定显示位置!的相关文章

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

跳转页面定位到指定的位置(转)

跳转页面定位到指定的位置 明天要回家了,今天也没啥事做.就分享下做过的一个小需求,还有很多朋友问过我类似的问题,当时也是跟他们说说简单的思路,并没有分享.今天我就整理下吧. 需求是这样的A页面有的链接,跳转到B页面中.并且要根据A页面的链接不同定位的不同的内容区域.我贴个图你大概就理解了: A页面 B页面 当我点击A页面中3的链接的时候,就要跳转到B页面,并且定位到活动三内容区域: 这个都是一个简单的活动页面,当然你也可以很简单的实现,把B页面拆分成3个页面,分别对应A页面的3个不容的内容,但是

除了创建时指定窗口位置之外,还有3种移动窗口位置的办法(移动的同时往往可以改变窗口大小)(SetWindowPos最有用,它有许多标志位)

首先,在创立窗口对象的时候,CreateWindowEx就可以指定窗口的位置.除此之外,还有三种方法可以改变窗口的位置: procedure TWinControl.CreateWindowHandle(const Params: TCreateParams); begin // 根据之前准备的Params参数使用API创建窗口.其10个参数都是Params的参数,0表示Menu,WindowClass的十项内容只用到了hInstance一项 // important 控件移到正确的显示位置,就

自定义PopupWindow实现3急地区联动

做项目时有时我们会需要3级联动,比如注册,买东西下单等,这里我在android上使用popupwindow实现3级联动功能,我实现的思路是,当程序启动时就将后台的地区JSON数据格式全部加载上来,通过SharedPreferences将获取到的数据保存,点击按钮获取SharedPreferences中的地区数据,再通过JSONObject转为List集合,具体实现如下: 布局文件: activity_main.xml: <LinearLayout xmlns:android="http:/

Android 自定义PopupWindow以及参数传递与返回

在这篇博客之前,还写了一篇关于PopupWindow,那篇主要是关于PopupWindow弹出位置的设置.以及选择PopupWindow布局后的监听.详情看Android popupwindow 示例程序一.接下来这篇主要是讲自定义PopupWindow以及参数传递与返回,我在里面写了一个listview来示例.接下来看代码,都有所注释. 本文项目资源下载: 一.MainActivity <span style="background-color: rgb(240, 240, 240);&

自定义PopupWindow弹出框(带有动画)

使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow 1 public class LostPopupWindow extends PopupWindow { 2 public Lost lost; 3 public void onLost(Lost lost){ 4 this.lost = lost; 5 } 6 private View conentView; 7 8 public View getConentView() { 9 return cone

分别用自定义PopupWindow和自定义Dialog实现下拉菜单

首先看下分别使用PopupWindow和Dialog实现的下拉菜单的不同之处: PopupWindow: Dialog: 由于之前用PopupWindow实现的效果不是太理想,并且弹出下拉菜单的时候背景透明度变化的也不是太好,后来改为Diaolog,项目中其他弹窗也都用的diaolog,便于更改背景透明度,整体看起来也比较统一. 下面把两种实现的方法都记录下来: **第一种:自定义PopupWindow** 首先自定义一个PopWindow: PopWindow.class: public cl

tabbar的BadgeValue 显示位置问题

abbar的BadgeValue 显示位置问题 1.在iOS 7.0的时候 tabbar item上的badgevalue显示正常 如图: 2.在iOS 7.1的时候 tabbar item上的badgevalue显示位置异常 如图: 不知道有没有解决办法. 设置代码:[[self.tabBar.items objectAtIndex:2] setBadgeValue:@"10"]; 及自定义tabbar中viewdidload部分代码: - (void)viewDidLoad { [