Android 自定义下拉/弹出菜单 [英] Android custom dropdown/popup menu
问题描述
如何创建一个固定到按钮的自定义下拉/弹出菜单?
How do I do a custom dropdown/popup menu anchored to a button?
我需要它像弹出菜单(锚定到视图)一样工作,并在我单击菜单中的项目时执行某些操作.
I need it to work like the popup menu (anchored to a view), and do something when I click an item from the menu.
如何通过代码向菜单添加项目,保持菜单的高度并使其在超过 5 个项目时可滚动.我不需要添加任何图片,只需添加文字.
How do I add items to the menu by code, keeping menu's height and make it scrollable if there are more than 5 items. I don't need to add any images, just text.
推荐答案
更新:要使用 Kotlin 在 android 中创建弹出菜单,请参考我的回答 此处.
Update: To create a popup menu in android with Kotlin refer my answer here.
使用 Java 在 android 中创建弹出菜单:
To create a popup menu in android with Java:
在res/layout
目录下创建一个布局文件activity_main.xml
,里面只有一个按钮.
Create a layout file activity_main.xml
under res/layout
directory which contains only one button.
文件名:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="62dp"
android:layout_marginTop="50dp"
android:text="Show Popup" />
</RelativeLayout>
在res/menu
目录下创建文件popup_menu.xml
它包含三个项目,如下所示.
It contains three items as shown below.
文件名:poupup_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/one"
android:title="One"/>
<item
android:id="@+id/two"
android:title="Two"/>
<item
android:id="@+id/three"
android:title="Three"/>
</menu>
MainActivity 类,在单击按钮时显示弹出菜单.
MainActivity class which displays the popup menu on button click.
文件名:MainActivity.java
public class MainActivity extends Activity {
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
MainActivity.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT
).show();
return true;
}
});
popup.show(); //showing popup menu
}
}); //closing the setOnClickListener method
}
}
以编程方式添加:
PopupMenu menu = new PopupMenu(this, view);
menu.getMenu().add("One");
menu.getMenu().add("Two");
menu.getMenu().add("Three");
menu.show();
按照此链接以编程方式创建菜单.
Follow this link for creating menu programmatically.
这篇关于Android 自定义下拉/弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!