【发布时间】:2011-10-08 10:38:39
【问题描述】:
那里的大多数示例都精确地指定了弹出窗口的宽度和高度。我希望它们是 WRAP_CONTENT - 因为内容是动态确定的,所以在构造函数中我为宽度和高度设置了 -2 并通过 showAsDropDown(View anchor)
显示它这样做,弹出窗口总是绘制在锚视图下方,这意味着它可以在屏幕外绘制。下面的 sn -p 演示了这个问题。尝试单击最后一个 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;
}
}
【问题讨论】:
标签: android