弹出窗口显示位置不准确,更改重力属性后仍然错误

4
当你点击文本视图时,我制作了一个弹出窗口,我按照指南创建弹出窗口,它能正常工作,只是弹出窗口的位置不是我想要的,似乎弹出窗口会将其左上角显示在指定位置,但我希望底部左侧角落在指定点,就像这样,在搜索后,我发现重力负责此操作,请参见 developer.android ,但是当我将重力更改为 Gravity.BOTTOM|Gravity.LEFT 时,弹出窗口显示在文本上方。
弹出窗口XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:id="@+id/popup"
    android:layout_height="wrap_content"

    android:orientation="vertical"
    android:gravity="center"
    android:padding="7dp"
    android:background="#d1a2a2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView37"
        android:gravity="center"
        android:textAlignment="gravity"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView41" />
</LinearLayout>

这里是我拥有的代码。
    vfy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //Open popup window
                if (p != null)
                  showPopup(MainActivity.this, p);
            }
    });

这些是方法。
@Override
    public void onWindowFocusChanged(boolean hasFocus) {

        int[] location = new int[2];
        TextView button = (TextView) findViewById(R.id.textView2);

        // Get the x, y location and store it in the location[] array
        // location[0] = x, location[1] = y.
        button.getLocationOnScreen(location);

        //Initialize the Point with x, and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];
    }

    //............
    private void showPopup(final Activity context, Point p) {


        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup, viewGroup);

        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
        int OFFSET_X = 30;
        int OFFSET_Y = 30;

        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

        // Getting a reference to Close button, and close the popup when clicked.
        TextView close = (TextView) layout.findViewById(R.id.textView37);
        close.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                popup.dismiss();
            }
        });
    }
1个回答

2

我正在使用showAtLocation()方法,但是当我改变重力时,它给出了错误的位置。 - tamim abweini
R.layout.popup和您上面提到的xml是相同的吗? - Ankur Samarya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接