您将如何在 Android 中创建弹出视图,例如 Facebook 评论?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23464232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would you create a popover view in Android, like Facebook Comments?
提问by David Xu
I was wondering if anyone knows how to create a Facebook-like popover view like in the Facebook Android app for comments.
我想知道是否有人知道如何在 Facebook Android 应用程序中创建类似 Facebook 的弹出视图以进行评论。
This is what I mean:
这就是我的意思:
Along with the handle that you can drag to dismiss it, is it a native Android UI control or has Facebook implemented this themselves?
连同您可以拖动以关闭它的手柄,它是原生的 Android UI 控件还是 Facebook 自己实现的?
回答by Libin
The best way to create similar popover view
is by using PopupWindow
, since you can place the PopUpWindow
on any of the specific view position (or on center/top/bottom of screen). You can also achieve same UI with DialogFragment
, but you cannot position at specific view location.
创建相似的最好方法popover view
是使用PopupWindow
,因为您可以将 放置PopUpWindow
在任何特定的视图位置(或屏幕的中心/顶部/底部)。您也可以使用 实现相同的 UI DialogFragment
,但不能定位在特定的视图位置。
I have a complete working code here https://gist.github.com/libinbensin/67fcc43a7344758390c3
我在这里有一个完整的工作代码https://gist.github.com/libinbensin/67fcc43a7344758390c3
Step 1:Create your custom layout , for e.g., as Facebook its has a Header TextView
with a ListView
and EditText
.
第 1 步:创建您的自定义布局,例如,因为 Facebook 有一个Header TextView
带有ListView
和的EditText
。
Step 2:Set the layout to the PopupWindow
第 2 步:将布局设置为PopupWindow
Inflate the layout to set
膨胀要设置的布局
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View inflatedView = layoutInflater.inflate(R.layout.fb_popup_layout, null,false);
This Layout
has a ListView
,so find the ListView
in the layout and fill the data . you can have your own view here
这Layout
有一个ListView
,所以ListView
在布局中找到并填充数据。你可以在这里有自己的看法
ListView listView = (ListView)inflatedView.findViewById(R.id.commentsListView);
listView.setAdapter(new ArrayAdapter<String>(TryMeActivity.this,
R.layout.fb_comments_list_item, android.R.id.text1,contactsList));
Now, create an instance of PopupWindow
with specific height and width. I prefer to set the size depends on the device.
现在,创建一个PopupWindow
具有特定高度和宽度的实例。我更喜欢根据设备设置大小。
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
popWindow = new PopupWindow(inflatedView, size.x - 50,size.y - 500, true );
Set the focusability
of the popup window.
设置focusability
弹出窗口的 。
popWindow.setFocusable(true);
Make it outside touchable to dismiss the popup window when touched outside
the popup area
使其to dismiss the popup window when touched outside
在弹出区域之外可触摸
popWindow.setOutsideTouchable(true);
Now, set a background to the PopupWindow
with a drawable. The drawable has rectangle shape
with corner radius
.
现在,PopupWindow
使用可绘制对象设置背景。提拉拥有rectangle shape
与corner radius
。
popWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.fb_popup_bg));
Finally. show the PopupWindow
at required location. I made it show at bottom
of the screen with some X and Y position
最后。显示PopupWindow
在所需的位置。我让它在bottom
屏幕上显示了一些X and Y position
popWindow.showAtLocation(v, Gravity.BOTTOM, 0,150); // 0 - X postion and 150 - Y position
You can also set an Animation
to use when the PopUpWindow
appears and disappears
您还可以设置Animation
在PopUpWindow
出现和消失时使用
popWindow.setAnimationStyle(R.anim.animation); // call this before showing the popup
回答by Bradley Campbell
Edit:
编辑:
Actually, even though I used a DialogFragment, I'm quite certain their popup does not use a DialogFragment (or even a Dialog at all!). The reason for this is the resizing feature. If that is something you want, then you can't use a DialogFragment. You would have to just add a new view to your layout. It looks like facebook also has another view that sits between your wall and the fake popup that is slightly translucent and listens for clicks in order to dismiss the view. Something like this would take some actual effort and time to build, so I won't make this one for you. Let me know if you have any questions about it though, I can probably guide you to the solution you are after.
实际上,即使我使用了 DialogFragment,我也很确定他们的弹出窗口没有使用 DialogFragment(甚至根本没有使用 Dialog!)。原因是调整大小功能。如果这是您想要的东西,那么您就不能使用 DialogFragment。您只需要在布局中添加一个新视图即可。看起来 facebook 也有另一个视图,它位于您的墙和稍微半透明的假弹出窗口之间,并侦听点击以关闭视图。像这样的东西需要一些实际的努力和时间来构建,所以我不会为你做这个。如果您对此有任何疑问,请告诉我,我可能会指导您找到所需的解决方案。
Original:
原来的:
I wrote the popup for you:
我为你写了弹出窗口:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
F1.newInstance().show(getFragmentManager(), null);
}
}
public static class F1 extends DialogFragment {
public static F1 newInstance() {
F1 f1 = new F1();
f1.setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_DeviceDefault_Dialog);
return f1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Remove the default background
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Inflate the new view with margins and background
View v = inflater.inflate(R.layout.popup_layout, container, false);
// Set up a click listener to dismiss the popup if they click outside
// of the background view
v.findViewById(R.id.popup_root).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return v;
}
}
}
popup_layout.xml:
popup_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:id="@+id/popup_root">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="72dp"
android:layout_marginBottom="72dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:padding="20dp"
android:clickable="true"
android:background="@drawable/dialog_background">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#000"
android:text="Content goes here!" />
</FrameLayout>
</FrameLayout>
And dialog_background.xml (goes into res/drawable):
和 dialog_background.xml(进入 res/drawable):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FFF" />
<corners android:topLeftRadius="20dp" android:topRightRadius="20dp"
android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp"/>
<stroke android:color="#7F7F7F" android:width="1dp" />
</shape>
And it looks like this:
它看起来像这样:
Just add your view content and you're good to go!
只需添加您的视图内容即可!
回答by Dave C
You can use a PopupWindow to do that. http://developer.android.com/reference/android/widget/PopupWindow.html
您可以使用 PopupWindow 来做到这一点。http://developer.android.com/reference/android/widget/PopupWindow.html
Of course you'll need to style it and fill in the popup's contents on your own. You can get some styling ideas from How to make layout with rounded corners..?
当然,您需要自己设置样式并填写弹出窗口的内容。您可以从How to make layout with rounded corners.. 中获得一些样式想法。
回答by Ramesh Prasad
This seems like a custom component built by Facebook. It is not a standard Android component. You can try implementing it by deriving from the Dialog Fragment.
这看起来像是 Facebook 构建的自定义组件。它不是标准的 Android 组件。您可以尝试通过从 Dialog Fragment 派生来实现它。
回答by danh32
It looks like it would be easiest to just use a Fragment with a transparent (or in this case translucent) background.
看起来最简单的方法是使用具有透明(或在本例中为半透明)背景的 Fragment。