有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在指定的线性布局上方显示弹出窗口

我试图在线性布局上方显示一个弹出窗口,但它没有显示在预期位置

这是我的主要线性布局和计算重力

这是我的密码:

public class MainActivity extends AppCompatActivity {

    Button button;
    PopupWindow popup;
    LinearLayout middle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        middle = (LinearLayout) findViewById(R.id.middle);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final View layout = getLayoutInflater().inflate(R.layout.mypopup, null);

                popup = new PopupWindow(layout);
                popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
                popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                popup.setOutsideTouchable(true);
                popup.setFocusable(true);
                popup.setBackgroundDrawable(new BitmapDrawable());

                popup.showAtLocation(middle, Gravity.TOP, 0, middle.getHeight());

                popup.showAsDropDown(middle);
            }
        });
    }
}

布局

<LinearLayout 安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical"
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">

            <LinearLayout
                安卓:id="@+id/viewA"
                安卓:layout_width="match_parent"
                安卓:layout_height="match_parent"
                安卓:layout_weight="0.5"
                安卓:background="#0094d6"
                安卓:orientation="horizontal"
                安卓:gravity="center_vertical|center_horizontal">

            </LinearLayout>

            <LinearLayout
                安卓:layout_width="match_parent"
                安卓:layout_height="wrap_content"
                安卓:background="#000000"
                安卓:paddingLeft="@dimen/activity_horizontal_margin"
                安卓:paddingStart="@dimen/activity_horizontal_margin"
                安卓:id="@+id/middle"
                安卓:orientation="horizontal">

                <Button
                    安卓:layout_width="wrap_content"
                    安卓:layout_height="wrap_content"
                    安卓:id="@+id/button"
                    安卓:text="show popup"/>

            </LinearLayout>

            <LinearLayout
                安卓:id="@+id/viewB"
                安卓:layout_width="match_parent"
                安卓:layout_height="match_parent"
                安卓:layout_weight="0.5"
                安卓:orientation="horizontal"
                安卓:background="#987f5a"/>

</LinearLayout>

弹出式布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:orientation="vertical" 安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:background="#ffffff">

    <TextView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="I'm a Popup"
        安卓:textSize="20dp"
        安卓:textStyle="bold"
        安卓:background="#000000"
        安卓:textColor="#ffffff"
        安卓:padding="20dp"/>

</LinearLayout>

问题是,弹出窗口显示在活动的顶部。我想在中间布局的顶部显示它

我哪里做错了? 多谢各位


共 (2) 个答案

  1. # 1 楼答案

    showPopupMenu中,只需传递您希望pop-up出现在其上的View

     private void showPopupMenu(View view) {
    
                PopupMenu popupMenu = new PopupMenu(mContext, view);
                MenuInflater inflater = popupMenu.getMenuInflater();
                inflater.inflate(R.menu.menu_overflow_extra, popupMenu.getMenu());
                popupMenu.show();
    
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.popup_share:
                                return true;
                            case R.id.popup_cast:
                                return true;
                        }
                        return false;
                    }
                });
            }
    
  2. # 2 楼答案

    你只需要给y赋值,它就会保持不变

    popup.showAtLocation(middle, Gravity.CENTER, 0, 24);