弹出窗口中的片段展示一些东西 [英] Pop up window to display some stuff in a fragment
问题描述
我试图使像一个弹出窗口,在片段的视图点击时会出现。我希望这个弹出窗口或什么的,不使该片段黑暗,就像一个对话片段一样。 而且我也不想被定位在弹出的观点被点击的地方。 将是一件好事,如果它有自己的活动和布局,所以我可以做它的一些自定义更改。 你能普莱舍给我一些示例code?
I am trying to make something like a pop-up window, that would appear when clicked on a view in a fragment. I want this pop-up window or whatever, to not make the fragment dark, like a Dialog Fragment does. And I also want the pop up to be positioned where the view is clicked. Would be good if it has its own activity and layout so I can do some custom changes in it. Can you plese show me some sample code?
推荐答案
以下应该正常工作,根据您的具体要求。从调用此方法在的onClick(视图v)
OnClickListener
分配给视图:
The following should work perfect in accordance with your specification. Call this method from inside onClick(View v)
of OnClickListener
assigned to the View:
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(视图v)
。
这篇关于弹出窗口中的片段展示一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!