我正在尝试在回收器视图中的特定视图上显示一个弹出窗口,但未在该位置显示。
这是我显示透明弹出窗口的步骤:
private void showPopup(View view) {
LayoutInflater inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.fadepopup, findViewById(R.id.fadePopup));
PopupWindow fadePopup = new PopupWindow(layout, 300, 500, true);
fadePopup.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent)));
fadePopup.setOutsideTouchable(true);
fadePopup.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER, 0, 0);
}
从回收者视图中调用它,例如:
horizontalRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
String menuTitle = menuTitles[position % menuTitles.length];
Fragment selectedFragment = null;
switch (menuTitle) {
case "Messenger":
showPopup(view);
break;
}
}
@Override
public void onLongClick(View view, int position) {
}
}));
此外,我尝试使用showAsDropDown
,但无法正常工作。
结果是:
更新:
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fadePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/messenger_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/circular_white"
android:padding="10dp"
android:src="@drawable/nav_messanger" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/circular_white"
android:padding="10dp"
android:src="@drawable/nav_contacts" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/circular_white"
android:padding="10dp"
android:src="@drawable/nav_boss" />
</LinearLayout>
答案 0 :(得分:0)
我认为您应该尝试使用[相对布局] [1]。 当您在整个屏幕上有相对布局时,将android:layout_alignParentBottom添加到XML并将Popup作为元素添加到使用setVisibility在fadePopup上显示的XML时,弹出窗口应显示在屏幕底部:使用setVisibility:Invisible可见和隐藏。因此,您基本上可以为弹出窗口添加自定义布局,并使用参数setVisibility:visible将其传递给fadePopup方法。
答案 1 :(得分:0)
这是showAtLocation()
中PopupWindow.java
的实现:
/**
* <p>
* Display the content view in a popup window at the specified location. If the popup window
* cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams}
* for more information on how gravity and the x and y parameters are related. Specifying
* a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying
* <code>Gravity.LEFT | Gravity.TOP</code>.
* </p>
*
* @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
* @param gravity the gravity which controls the placement of the popup window
* @param x the popup's x location offset
* @param y the popup's y location offset
*/
public void showAtLocation(View parent, int gravity, int x, int y) {
mParentRootView = new WeakReference<>(parent.getRootView());
showAtLocation(parent.getWindowToken(), gravity, x, y);
}
如您所见,参数parent
是将弹出窗口显示到的根视图,类似于活动的布局。
但是,您需要为该参数传递在弹出窗口内膨胀的布局!
修改:
您必须通过要在其上锚定弹出窗口的视图,并且x,y将是其位置的偏移量。