大多数示例都指定了弹出窗口的宽度和高度。我想他们是WRAP_CONTENT - 由于内容dinamically决定的,所以在构造函数中我设置-2宽度和高度,并通过showAsDropDown(查看锚)未指定大小时弹出窗口弹出窗口
这样做表明它,在弹出总是画在锚点视图下方,这意味着它可以在屏幕外画出。以下片段演示了这个问题。尝试点击最后一个TextView,您将看不到任何PopupWindow,因为它显示在窗口范围之外。为什么它不起作用?我指出明确指定维度(例如200,100)不会触发问题。试一试
package com.zybnet.example.popupdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class PopupDemoActivity extends Activity implements OnClickListener {
private PopupWindow popup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// -2 means WRAP_CONTENT THIS TRIGGERS THE PROBLEM
popup = new PopupWindow(getPopupContent(), -2, -2);
// When you specify the dimensions everything goes fine
//popup = new PopupWindow(getPopupContent(), 200, 100);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// FILL_PARENT and same layout weight for all children
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -1, 1);
for (int i = 0; i < 10; i++) {
TextView tv = new TextView(this);
tv.setText("Click to show popup");
tv.setOnClickListener(this);
layout.addView(tv, params);
}
setContentView(layout);
}
@Override
public void onClick(View view) {
popup.dismiss();
popup.showAsDropDown(view);
}
private View getPopupContent() {
TextView popupContent = new TextView(this);
popupContent.setText("Some text here");
popupContent.setTextColor(Color.parseColor("#5000ae"));
popupContent.setBackgroundColor(Color.parseColor("#ff00ff"));
popupContent.setPadding(10, 20, 20, 10);
return popupContent;
}
}
我读得源,它似乎更多的A ndroid的bug,因为大小是正确的,但放置是错误的。自从我提供演示以来,您可以亲自测试它。现在我正在试验PopupWindow.showAtLocation() – Raffaele
正如我怀疑你需要在'setWindowLayoutMode'中添加我编辑我的答案并添加到您需要的确切工作代码。我在我的android设备上测试了它,所有东西! :) – Bob
如果下一次调用弹出内容视图更改会发生什么?这就是为什么我需要WRAP_CONTENT – Raffaele