【发布时间】: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