如何将自定义菜单添加到底部应用栏?
How to Add custom menu to Bottom App Bar?
我在 android 中使用新的 Material 底部应用栏。我已成功实施,但我不知道如何将自定义菜单项添加到栏中。每当我添加菜单项时,即使我提供选项 android:showAsAction="always",它们也只会显示为 3 个点。
我想要特定的图标,如下面的屏幕截图。
但是相反,我得到了这样的结果。
这是布局代码。
<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"/>
您可以为此使用弹出菜单。放置一个菜单图像,然后单击图像打开 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();
您可以关注下方link:
https://www.javatpoint.com/android-popup-menu-example
尝试在 BottomAppBar 中将 fabCradleDiameter 设置为 0db,例如:
app:fabCradleDiameter="0dp"
使用android:showAsAction="ifRoom"
我让它与新的 Material 底部应用栏一起使用以显示图标。
https://developer.android.com/guide/topics/resources/menu-resource
经过大量研究,我终于找到了解决问题的方法。只需将 'showAsAction' 的命名空间从 'android' 更改为 'app' 即可使事情正常进行。
<item
android:id="@+id/navigation_explore"
android:icon="@drawable/ic_search"
android:title="Explore"
app:showAsAction="always" />
我在 android 中使用新的 Material 底部应用栏。我已成功实施,但我不知道如何将自定义菜单项添加到栏中。每当我添加菜单项时,即使我提供选项 android:showAsAction="always",它们也只会显示为 3 个点。
我想要特定的图标,如下面的屏幕截图。
这是布局代码。
<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"/>
您可以为此使用弹出菜单。放置一个菜单图像,然后单击图像打开 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();
您可以关注下方link: https://www.javatpoint.com/android-popup-menu-example
尝试在 BottomAppBar 中将 fabCradleDiameter 设置为 0db,例如:
app:fabCradleDiameter="0dp"
使用android:showAsAction="ifRoom"
我让它与新的 Material 底部应用栏一起使用以显示图标。
https://developer.android.com/guide/topics/resources/menu-resource
经过大量研究,我终于找到了解决问题的方法。只需将 'showAsAction' 的命名空间从 'android' 更改为 'app' 即可使事情正常进行。
<item
android:id="@+id/navigation_explore"
android:icon="@drawable/ic_search"
android:title="Explore"
app:showAsAction="always" />