android - 您将如何在 Android 中创建弹出 View ,例如 Facebook 评论?

标签 android android-layout android-fragments

我想知道是否有人知道如何在 Facebook Android 应用程序中创建类似 Facebook 的弹出式 View 以供评论。

这就是我的意思:

enter image description here

除了可以拖动以将其关闭的句柄之外,它是原生 Android UI 控件还是 Facebook 自己实现的?

最佳答案

创建类似popover view 的最佳方法是使用PopupWindow ,因为您可以将 PopUpWindow 放置在任何特定的 View 位置(或屏幕的中心/顶部/底部)。您也可以使用 DialogFragment 实现相同的 UI,但您不能定位在特定的 View 位置。

我这里有一个完整的工作代码 https://gist.github.com/libinbensin/67fcc43a7344758390c3

第 1 步: 创建您的自定义布局,例如,Facebook 有一个 Header TextView 和一个 ListViewEditText .

第 2 步: 将布局设​​置为 PopupWindow

膨胀布局来设置

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

这个Layout有一个ListView,所以在layout中找到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));

现在,创建一个具有特定高度和宽度的 PopupWindow 实例。我更喜欢根据设备设置大小。

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

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

设置弹窗的focusability

popWindow.setFocusable(true);

使其在可触摸的外部在触摸外部时关闭弹出窗口弹出区域

popWindow.setOutsideTouchable(true);

现在,使用可绘制对象为 PopupWindow 设置背景。可绘制对象具有 rectangle 形状corner radius

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

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

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

你也可以设置一个AnimationPopUpWindow出现和消失时使用

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

enter image description here

关于android - 您将如何在 Android 中创建弹出 View ,例如 Facebook 评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23464232/

相关文章:

android - 两个方向的 ScrollView

android - 在另一个 fragment 中使用 fragment 实现 TabLayout

java - 屏幕旋转后未调用 onSaveInstanceState

java - Android 刷新抽屉导航 fragment ?

android - 我的应用程序停留在 "draft"模式(Play 商店)

Android View 比较两个 View 是否相等

android - 当我更改广告时 :adUnitId to my own string id the ad is not shown

android - RecyclerView 在调用 notifyDataSetChanged() 时跳到顶部

java - 如何处理 ExpandableListView 子级的一行中不同 View 的单击事件

android - ScrollView 内部的 FrameLayout