你会如何​​创建一个Android的观点popover,如Facebook评论?你会、创建一个、观点、Android

2023-09-05 05:37:00 作者:举悲到凌尘ゅ

我想知道是否有人知道如何创建像Facebook的Andr​​oid应用程序征求意见一个类似Facebook的popover看法。

I was wondering if anyone knows how to create a Facebook-like popover view like in the Facebook Android app for comments.

这是我的意思:

随着你可以通过拖动来关闭它的手柄,它是一个原生的Andr​​oid UI控件或已Facebook的实现了这个自己呢?

Along with the handle that you can drag to dismiss it, is it a native Android UI control or has Facebook implemented this themselves?

推荐答案

创建一个类似于 popover视图最好的办法是使用 PopupWindow ,因为你可以将 PopUpWindow 在任何特定视图位置(或在屏幕中央/顶/底部)。您也可以达到相同的用户界面与 DialogFragment ,但你不能位置特定视图位置。

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.

我有一个完整的工作code此处https://gist.github.com/libinbensin/67fcc43a7344758390c3

I have a complete working code here https://gist.github.com/libinbensin/67fcc43a7344758390c3

步骤1:的创建自定义布局,为例如,像Fac​​ebook的有一个头的TextView 的ListView 的EditText

Step 1: Create your custom layout , for e.g., as Facebook its has a Header TextView with a ListView and EditText.

第2步:的布局设置为 PopupWindow

膨胀的布局设置的

LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View inflatedView = layoutInflater.inflate(R.layout.fb_popup_layout, null,false);

布局有一个的ListView ,所以找到的ListView 在布局和填充数据。你可以在这里有自己的看法。

This Layout has a ListView ,so find the ListView in the layout and fill the data . you can have your own view here

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));

现在,创建 PopupWindow 与特定的高度和宽度的一个实例。我preFER设置大小取决于设备。

Now, create an instance of PopupWindow with specific height and width. I prefer to set the size depends on the device.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

popWindow = new PopupWindow(inflatedView, size.x - 50,size.y - 500, true );

设置的可聚焦的弹出窗口。

popWindow.setFocusable(true);

请它外面触摸辞退时,外面触摸弹出窗口在弹出的区域

Make it outside touchable to dismiss the popup window when touched outside the popup area

popWindow.setOutsideTouchable(true);

现在,设置背景的 PopupWindow 与绘制。可绘制的有矩形圆角半径

Now, set a background to the PopupWindow with a drawable. The drawable has rectangle shape with corner radius.

  popWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.fb_popup_bg));

最后。显示 PopupWindow 在需要的位置。我把它显示在一些 X和Y位置的屏幕

Finally. show the PopupWindow at required location. I made it show at bottom of the screen with some X and Y position

popWindow.showAtLocation(v, Gravity.BOTTOM, 0,150);  // 0 - X postion and 150 - Y position

您还可以设置一个动画时要使用 PopUpWindow 出现和消失

You can also set an Animation to use when the PopUpWindow appears and disappears

popWindow.setAnimationStyle(R.anim.animation); // call this before showing the popup