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>
问题是,弹出窗口显示在活动的顶部。我想在中间布局的顶部显示它
我哪里做错了? 多谢各位
# 1 楼答案
在showPopupMenu中,只需传递您希望
pop-up
出现在其上的View
:# 2 楼答案
你只需要给y赋值,它就会保持不变