我正在尝试制作类似于弹出窗口的东西,当单击片段中的视图时会出现该窗口。我希望这个弹出窗口或其他任何东西不会像对话框片段那样使片段变暗。而且我还希望弹出窗口位于单击视图的位置。如果它有自己的活动和布局会很好,这样我就可以在其中进行一些自定义更改。你能告诉我一些示例代码吗?
问问题
37971 次
1 回答
51
以下应该根据您的规范完美运行。onClick(View v)
从OnClickListener
分配给视图的内部调用此方法:
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// Example: If you have a TextView inside `popup_layout.xml`
TextView tv = (TextView) popupView.findViewById(R.id.tv);
tv.setText(....);
// Initialize more widgets from `popup_layout.xml`
....
....
// If the PopupWindow should be focusable
popupWindow.setFocusable(true);
// If you need the PopupWindow to dismiss when when touched outside
popupWindow.setBackgroundDrawable(new ColorDrawable());
int location[] = new int[2];
// Get the View's(the one that was clicked in the Fragment) location
anchorView.getLocationOnScreen(location);
// Using location, the PopupWindow will be displayed right under anchorView
popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
}
评论应该很好地解释了这一点。anchorView
是v
从onClick(View v)
。
于 2013-08-27T09:56:34.753 回答