Android开发 - 如何制作横向弹出菜单 [英] Android development - how to make horizontal popup menu
问题描述
我正在研究简单的Android应用程序,并希望在单击ImageButton之后以水平方式显示弹出菜单(溢出菜单)(所有项目都以单行对齐),如下所示 - 图片 [ ^ ]。有什么方法可以实现吗?
我的代码:
MainActivity.class
I'm working on simple Android app and want the Popup Menu(Overflow Menu) to be shown in Horizontal way (all items aligned in single line) after I click the ImageButton, something like this - Image[^]. Is there any way to make it happen?
My Code :
MainActivity.class
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button to show PopupMenu
ImageButton buttonPlus = (ImageButton) findViewById(R.id.popupButton);
popupMenu = new PopupMenu(this, buttonPlus);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.menu, popupMenu.getMenu());
buttonPlus.setOnClickListener(new ImageButton.OnClickListener()
{
public void onClick(View v)
{
popupMenu.show();
}
});
}
menu.xml
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.captain.pc_1.MainActivity">
<group android:checkableBehavior="single">
<item
android:id="@+id/_N1"
android:orderInCategory="100"
android:title="N1"
app:showAsAction="never"/>
<item
android:id="@+id/_M2"
android:orderInCategory="101"
android:title="N2"
app:showAsAction="never"/>
</group>
</menu>
以上鳕鱼e给出的输出看起来像这个
我曾尝试过:
我看到很多帖子与我的问题有关,但都没有成功。他们中的大多数人都要求使用HorizontalScrollView,但这不是我想要的。任何帮助将不胜感激。
The Above code gives output looks something like this
What I have tried:
I saw many post related to my question but none of them worked out. Most of them asked to use HorizontalScrollView but it's not what I want. Any Help will be appreciated.
推荐答案
如果您想要弹出窗口的自定义布局,您必须使用 PopupWindow [ ^ ]
尝试以下演示:
1.创建一个这样的popup.xml:
If you want to have custom layout for popup, you have to use PopupWindow[^]
Try out the following demo:
1. Create a popup.xml like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:removed="#f46464">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/radioGroupGender"
android:layout_weight="2">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="@+id/radFemale"
android:layout_gravity="center_vertical" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="@+id/radMale"
android:layout_gravity="center_vertical" />
</RadioGroup>
</LinearLayout>
2.将以下代码添加到onClick()方法:
2. Add the following code to the onClick() method:
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
View parent = view.getRootView();
popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
您将弹出水平对齐的单选按钮。当你在弹出式菜单中点击它之外它会关闭。
You will get a pop up with horizontal aligned radio buttons. It closes when you click outside of it like a pop up menu.
这篇关于Android开发 - 如何制作横向弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!