我只想在Gmail应用中实现与弹出式菜单相同的功能,并固定在右上角的溢出按钮上。为此,我使用与Android Android popup menu的谷歌教程相同的代码,但对我来说,在动作栏的边缘顶部显示弹出菜单。如果你在gmail溢出的pop菜单上注意到你看到popmenu发生在actionbar的边缘。
这是我用于弹出菜单的xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item1"
android:title="lablab"/>
<item
android:id="@+id/item2"
android:title="lablab"/>
</menu>
以下是我的活动:
public void showFontSetting(View view) {
PopupMenu popup = new PopupMenu(this, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(Index.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
break;
}
return true;
}
});
}
答案 0 :(得分:40)
PopupMenu popupMenu = new PopupMenu(getContext(), this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0);
styles.xml
<style name="PopupMenuOverlapAnchor" parent="@style/Theme.AppCompat.Light">
<item name="android:overlapAnchor">true</item>
<item name="android:dropDownVerticalOffset">0dp</item>
<item name="android:dropDownHorizontalOffset">0dp</item>
</style>
代码:
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenuOverlapAnchor);
PopupMenu popupMenu = new PopupMenu(contextThemeWrapper, this);
不再有效了:
这是一种调整 PopupMenu
位置的简单方法。它将菜单定位在其锚点视图(overflowButton
)上,就像操作栏中的菜单一样:
PopupMenu popupMenu = new PopupMenu(context, overflowMenuButton);
popupMenu.inflate(R.menu.my_menu);
// Fix vertical offset
overflowMenuButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
popupMenu.show();
if (popupMenu.getDragToOpenListener() instanceof ListPopupWindow.ForwardingListener)
{
ListPopupWindow.ForwardingListener listener = (ListPopupWindow.ForwardingListener) popupMenu.getDragToOpenListener();
listener.getPopup().setVerticalOffset(-overflowMenuButton.getHeight());
listener.getPopup().show();
}
}
});
击> <击> 撞击>
答案 1 :(得分:5)
在我的情况下应用重力
EmployeePresentMessage
答案 2 :(得分:2)
将以下代码添加到您的活动中:
PopupWindow popupwindow_obj; // create object
popupwindow_obj=popupDisplay(); // initialize in onCreate()
popupwindow_obj.showAsDropDown(clickbtn,-40, 18); // where u want show on view click event
public PopupWindow popupDisplay() { // disply designing your popoup window
final PopupWindow popupWindow = new PopupWindow(this); // inflet your layout or diynamic add view
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
Button item = (LinearLayout) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
在res/layout
目录中创建XML并将其命名为mylayout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>
答案 3 :(得分:0)
popupwindow.showAsDropDown(view,x,y);
此处 x 和 y 是弹出窗口相对于您的视图的位置。
答案 4 :(得分:0)
尝试了我发现的每种方法之后,我认为放置锚点视图可能仍然是一种更轻松,更简单的方法,尤其是当您使用更灵活的布局时,例如ConstraintLayout。
只需在希望弹出菜单定位的位置放置一个不可见视图:
<View
android:id="@+id/anchor_menu"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="@+id/button_menu"
app:layout_constraintTop_toBottomOf="@+id/button_menu"
/>
然后将其用作锚定视图:
mPopupMenu = new PopupMenu(getActivity(), mPopupMenuAnchor);
景气,完成了。