【问题标题】:How to Add custom menu to Bottom App Bar?如何将自定义菜单添加到底部应用栏?
【发布时间】:2018-07-18 10:47:44
【问题描述】:

我在 android 中使用新的 Material Bottom 应用栏。我已经成功实现它,但我不知道如何将自定义菜单项添加到栏。每当我添加菜单项时,它们仅显示为 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"/>

【问题讨论】:

    标签: android material-design android-bottomappbar


    【解决方案1】:

    经过这么多研究,我终于找到了解决问题的方法。只需将“showAsAction”的命名空间从“android”更改为“app”即可。

    <item
        android:id="@+id/navigation_explore"
        android:icon="@drawable/ic_search"
        android:title="Explore"
        app:showAsAction="always" />
    

    【讨论】:

      【解决方案2】:

      使用android:showAsAction="ifRoom"

      我让它与新的 Material 底部应用栏一起显示图标。

      https://developer.android.com/guide/topics/resources/menu-resource

      【讨论】:

      • 我已经试过了,然后又试了一次,但它不起作用。
      【解决方案3】:

      尝试在 BottomAppBar 中将 fabCradleDiameter 设置为 0db,例如: app:fabCradleDiameter="0dp"

      【讨论】:

      • 不确定这是否回答了所提出的问题。您能否发布代码并在可能的情况下将屏幕截图作为答案的一部分?
      【解决方案4】:

      您可以为此使用弹出菜单。放置一个菜单图像,点击图像打开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

      【讨论】:

      • 这与我想做的完全相反。我希望我的菜单项的特定图标不会像上传的屏幕截图那样弹出。正确阅读问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 2023-02-10
      • 1970-01-01
      • 2012-10-24
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多