我正在使用android中的新材料底部应用程序栏。我已经成功地实现了它,但我不知道如何向工具栏添加自定义菜单项。每当我添加菜单项时,它们只显示为3个点,即使我提供了选项android:showAsAction="always“。
我想要像下面的截图那样的具体图标。
但我得到的结果是这样的。
这是布局代码。
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
style="@style/Widget.MaterialComponents.BottomAppBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
app:backgroundTint="@color/colorPrimaryDark"
app:fabCradleMargin="5dp"
app:fabAlignmentMode="center"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottom_app_bar" />
这是java代码。
BottomAppBar bottomAppBar = (BottomAppBar) findViewById(R.id.bottom_app_bar);
setSupportActionBar(bottomAppBar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
return true;
}
菜单代码。
<item
android:id="@+id/navigation_explore"
android:icon="@drawable/explore"
android:title="Explore"
android:showAsAction="always"/>
<item
android:id="@+id/navigation_profile"
android:icon="@drawable/profile"
android:title="Profile"
android:showAsAction="always"/>
发布于 2018-08-21 12:24:05
经过这么多的研究,我终于找到了解决问题的办法。只需将'showAsAction‘的名称空间从'android’改为'app‘,就可以让一切正常工作。
<item
android:id="@+id/navigation_explore"
android:icon="@drawable/ic_search"
android:title="Explore"
app:showAsAction="always" />
发布于 2018-08-21 06:42:18
使用android:showAsAction="ifRoom"
我让它与新材料底部应用程序栏一起工作,以显示图标。
https://developer.android.com/guide/topics/resources/menu-resource
发布于 2018-07-18 19:29:24
你可以使用弹出菜单来实现这一点。放置一个菜单图像,单击图像打开android的弹出菜单,它与菜单相同。
PopupMenu popup = new PopupMenu(MainActivity.this, button);
//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();
你可以点击下面的链接:https://www.javatpoint.com/android-popup-menu-example
https://stackoverflow.com/questions/51400066
复制相似问题