首页 > Android PopupWindow的showAtLocation遇到了一个很神奇的问题!!要多加50px才对

Android PopupWindow的showAtLocation遇到了一个很神奇的问题!!要多加50px才对

这是我的主界面:

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:context=".MainActivity">


   
    <RelativeLayout
        android:id="@+id/rl_main_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:background="@color/light_blue">

        <TextView
            android:id="@+id/tv_titlebar_main_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="会员查询"
            android:textColor="#fff"
            android:textSize="18sp" />

        <Button
            android:id="@+id/iv_titlebar_main_settings"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentRight="true"
            android:background="@android:color/holo_green_dark"
            android:padding="7dp" />

    </RelativeLayout>


    <LinearLayout
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_main_showCLeft"
            style="@style/FuncButton"
            android:text="往左偏移" />



    </LinearLayout>


</LinearLayout>

我想在绿色块的下方显示popupWindow
然后我这么写:

@Bind(R.id.rl_main_test)
RelativeLayout rlTest;

public void click(View view) {
        switch (view.getId()) {
            case R.id.btn_main_showCLeft:
                int height = rlTest.getHeight();
                popup.showAtLocation(rlTest, Gravity.TOP | Gravity.RIGHT, 10, height);
                break;
        }
    }

结果是:

然后我增加了50px:
就变成正好了。。。

public void click(View view) {
        switch (view.getId()) {
            case R.id.btn_main_showCLeft:
                int height = rlTest.getHeight();
                popup.showAtLocation(rlTest, Gravity.TOP | Gravity.RIGHT, 10, height + 50);
                break;
        }
    }
    

我想知道这个50px怎么来的!


并不是因为你加了50就刚刚好,而是在你当前的机器上50可能刚好,到别的机器上就不一定了。showAtLocation中的x, y是屏幕的x, y,所以准确的y应该是(y + 状态栏高度)。

【热门文章】
【热门文章】