Pop up window to display some stuff in a fragment(弹出窗口以在片段中显示一些东西)
问题描述
我正在尝试制作类似于弹出窗口的东西,当单击片段中的视图时会出现该窗口.我希望这个弹出窗口或其他任何东西不会像对话框片段那样使片段变暗.而且我还希望弹出窗口位于单击视图的位置.如果它有自己的活动和布局会很好,这样我就可以在其中进行一些自定义更改.可以给我看一些示例代码吗?
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?
推荐答案
以下内容应符合您的规范.从分配给视图的 OnClickListener
的 onClick(View v)
内部调用此方法:
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
是 onClick(View v)
中的 v
.
The comments should explain this well enough. anchorView
is the v
from onClick(View v)
.
这篇关于弹出窗口以在片段中显示一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:弹出窗口以在片段中显示一些东西
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01