search

Home  >  Q&A  >  body text

android-studio - 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怎么来的!

PHPzPHPz2772 days ago592

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 15:35:51

    It’s not that just because you add 50, it’s just right, but that 50 may be just right on your current machine, but not necessarily on other machines. The x, y in showAtLocation are the x, y of the screen, so the accurate y should be (y + status bar height).

    reply
    0
  • Cancelreply