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

时间:2016-08-31 14:37:33

标签: java android xml android-layout

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

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

这是我的代码:

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的顶部。我想在中间布局的顶部显示它。

我在哪里做错了? 谢谢

2 个答案:

答案 0 :(得分:0)

showPopupMenu 中,只需传递您希望if metadataObj.stringValue != nil { dispatch_async(dispatch_get_main_queue()) { self.performSegueWithIdentifier("SendDataSegue", sender: self) } func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "SendDataSegue" { if let sendToDetailViewController = segue.destinationViewController as? DetailViewController { var sendingText = metadataObj.stringValue sendToDetailViewController.viaSegue = sendingText } } } 出现的View

pop-up

答案 1 :(得分:0)

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

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