我看到过类似的问题,但大多数解决方案都围绕着拆分操作栏。我认为这在我的应用程序中是不可能的,因为我的主题是Theme.Material.Light.NoActionBar
。
下面是我想做的一个例子。溢出菜单位于右下角,单击时,会打开一个类似于正常选项菜单的ListView
,示例如下。
我不确定是否可以使用像onCreateOptionsMenu
和onOptionsItemSelected
这样的本机函数。没有操作栏我该怎么办?嵌套的片段能工作吗?
我的应用程序的布局是ViewPager
中的片段。我的溢出菜单按钮的代码是直接的,但我不知道如何在onClick
方法中打开ListView
:
mSettingsButton = (Button) findViewById(R.id.settingsButton);
mSettingsButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
如果有任何建议或例子,我将不胜感激。谢谢
您可以使用Toolbar类,并根据需要在屏幕上对其进行样式/定位。
例如
<android.support.v7.widget.Toolbar
android:id=”@+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/colorPrimary” />
之后,使用
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
将其设置为您的主要操作栏。onCreateOptionsMenu
和其他与操作栏相关的功能将按预期工作。确保您也禁用了主题中的默认操作栏。
您可以通过按钮为ListView使用ListPopupWindow
例如:
ListPopupWindow popup = new ListPopupWindow(SomeActivity.this);
popup.setAdapter(new ArrayAdapter(SomeActivity.this,R.layout.list_item, someSettings));
popup.setAnchorView(settingsButton);
popup.setWidth(300);
popup.setHeight(400);
popup.setModal(true);
popup.setOnItemClickListener(SomeActivity.this);
settingsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
popup.show();
}
});