【问题标题】:How to show a Popup Window above specified Linear Layout如何在指定的线性布局上方显示弹出窗口
【发布时间】:2016-08-31 14:37:32
【问题描述】:

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

它正在采用我的主要线性布局并计算重力。

这是我的代码:

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 android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

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

            </LinearLayout>

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

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

            </LinearLayout>

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

</LinearLayout>

弹出布局:

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

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

</LinearLayout>

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

我哪里做错了? 谢谢你

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    showPopupMenu中只需传递你的View,你想让pop-up出现就可以了:

     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】:

      你只需要给 y 赋予价值:它会保持这种状态

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多