1

我正在使用带有图像按钮的 BottomAppBar。下面是bottomapbar的代码。

<com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bar"
            style="@style/Widget.MaterialComponents.BottomAppBar"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/bottom_tab_height"
            android:layout_gravity="bottom"
            android:backgroundTint="@color/materialBlue"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:fabAlignmentMode="end"
            app:fabCradleRoundedCornerRadius="@dimen/activity_horizontal_margin3"
            app:hideOnScroll="true"
            >
            <include
                android:id="@+id/nav_bar"
                layout="@layout/bottom_navigation_bar" />
        </com.google.android.material.bottomappbar.BottomAppBar>

bottom_navigation_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:paddingRight="16dp"
        android:weightSum="4"
       >

        <ImageButton
            android:id="@+id/home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackgroundBorderless"
            app:srcCompat="@drawable/ic_home_black_24dp" />

        <ImageButton
            android:id="@+id/best_selling"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackgroundBorderless"
            app:srcCompat="@drawable/ic_add_shopping_cart_black_24dp" />


        <ImageButton
            android:id="@+id/settings_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackgroundBorderless"
            app:srcCompat="@drawable/ic_settings_black_24dp" />
    </LinearLayout>
</layout>

我很难突出显示选定的 ImageButtons。我试图手动捕捉选择事件并突出显示它们。但是有没有更好的方法在 BottomAppBar 中选择图像按钮?

4

0 回答 0