我有一个带有按钮的工具栏,可以打开PopupMenu,但我希望它在它下面打开。(http://openclassrooms.com/forum/sujet/android-personnaliser-la-popupmenu)
让我解释一下,当我按下工具栏的右键时,我的弹出窗口将打开(随动画一起运行),它位于屏幕下方,即整个屏幕的整个宽度。我无法将单个弹出窗口移动到我想要的位置。
PopupMenu是否受定制限制?在这种情况下,有没有办法创建我的组合?
先谢谢您。
News.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actualite);
// Initializing
toolbar = (Toolbar) findViewById(R.id.toolbar);
button_popup_actu = (Button) findViewById(R.id.show_list_news);
button_popup_actu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(News.this, button_popup_actu);
popup.getMenuInflater().inflate(R.menu.popup_menu_news, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(News.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
}
/ menu / Popup_menu_new.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/news"
android:title="News"/>
<item
android:id="@+id/work"
android:title="Works"/>
<item
android:id="@+id/about"
android:title="About"/>
</menu>
/ layout / row_list_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/icon_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="||"/>
<TextView
android:id="@+id/title_row_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/number_none_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
</LinearLayout>
通常,您无法自定义PopupMenu的布局。在下面的解决方案中,我创建了一个具有水平布局的PopupMenu。在这种情况下,我将TextView用作可单击项,但是您可以轻松地用Button替换它们。
1-自定义PopupMenu类:
public class PopupMenuCustomLayout {
private PopupMenuCustomOnClickListener onClickListener;
private Context context;
private PopupWindow popupWindow;
private int rLayoutId;
private View popupView;
public PopupMenuCustomLayout(Context context, int rLayoutId, PopupMenuCustomOnClickListener onClickListener) {
this.context = context;
this.onClickListener = onClickListener;
this.rLayoutId = rLayoutId;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(rLayoutId, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true;
popupWindow = new PopupWindow(popupView, width, height, focusable);
popupWindow.setElevation(10);
LinearLayout linearLayout = (LinearLayout) popupView;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
View v = linearLayout.getChildAt(i);
v.setOnClickListener( v1 -> { onClickListener.onClick( v1.getId()); popupWindow.dismiss(); });
}
}
public void setAnimationStyle( int animationStyle) {
popupWindow.setAnimationStyle(animationStyle);
}
public void show() {
popupWindow.showAtLocation( popupView, Gravity.CENTER, 0, 0);
}
public void show( View anchorView, int gravity, int offsetX, int offsetY) {
popupWindow.showAsDropDown( anchorView, 0, -2 * (anchorView.getHeight()));
}
public interface PopupMenuCustomOnClickListener {
public void onClick(int menuItemId);
}
}
2-您的自定义布局,例如具有水平布局的linearlayout。在这种情况下,我将简单的LinearLayout与TextView项一起使用。您可以使用按钮等。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="horizontal"> <TextView android:id="@+id/popup_menu_custom_item_a" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="A" android:textAppearance="?android:textAppearanceMedium" /> <TextView android:id="@+id/popup_menu_custom_item_b" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:text="B" android:textAppearance="?android:textAppearanceMedium" /> // ... </LinearLayout>
3-使用自定义PopupMenu像普通的PopupMenu。
PopupMenuCustomLayout popupMenu = new PopupMenuCustomLayout(
MainActivity.mainActivity, R.layout.popup_menu_custom_layout,
new PopupMenuCustomLayout.PopupMenuCustomOnClickListener() {
@Override
public void onClick(int itemId) {
// log statement: "Clicked on: " + itemId
switch (itemId) {
case R.id.popup_menu_custom_item_a:
// log statement: "Item A was clicked!"
break;
}
}
});
// Method 1: popupMenu.show();
// Method 2: via an anchor view:
popupMenu.show( anchorView, Gravity.CENTER, 0, 0);