1

我目前正在使用BottomAppBar我的音乐应用程序,左侧有 4 个菜单项,右侧固定 FAB,每个项目都将导航到一个功能片段。

我想为所有 4 个菜单项设置一个选择器,类似于BottomNavigationView在选择和未选择时将有 2 个状态。但到目前为止,我正在努力做到这一点。

我已经尝试了 XML 中的选择器或以编程方式在代码中设置它,但这些都不起作用。

这是我想要实现的布局,抱歉不能发布直接图像,因为我没有足够的声誉。
https://imgur.com/972GZX8

4

2 回答 2

2

编辑1:

Okay great, so I just checked it out and you will not be able to change the color of icons in the bottomappbar when selected similar to BottomNavigationView. 但是有一个技巧可以在其中添加一个BottomNavigationView..BottomAppBar

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        app:backgroundTint="#008577"
        app:fabAlignmentMode="end">

           <com.google.android.material.bottomnavigation.BottomNavigationView
              android:id="@+id/navigation1"
              android:layout_width="match_parent"
              android:layout_height="56dp"
              android:background="?android:attr/windowBackground"
              app:itemBackground="@android:color/white"
              app:itemIconTint="@drawable/nav_item_colors"
              app:itemTextColor="@drawable/nav_item_colors"
              app:menu="@menu/navigation" />

  </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_attach_money_black_24dp"
        app:layout_anchor="@id/bottom_app_bar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

在您的情况下,创建五个菜单项并禁用 bnv 中的第五个菜单项。所以你的工厂会出现在那个地方。

老答案:

试试这个

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navigation1"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:itemBackground="@android:color/white"
    app:itemIconTint="@drawable/nav_item_colors"
    app:itemTextColor="@drawable/nav_item_colors"
    app:menu="@menu/navigation" />

可绘制目录中,创建nav_item_colors

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#E15B49" android:state_checked="true" />
    <item android:color="@color/colorviolet" android:state_checked="false" />
</selector>
于 2019-05-16T05:12:21.480 回答
0

尝试这个,

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            style="@style/Widget.MaterialComponents.BottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/gray_f1f0f5"
            app:itemTextColor="@color/nav_item_colors"
            app:labelVisibilityMode="labeled"
            app:layout_constraintBottom_toBottomOf="parent"
            app:menu="@menu/bottom_navigation_menu" />
于 2019-05-16T05:51:56.857 回答