如何在Android BottomNavigationView上获得方形效果而不是波纹?

时间:2017-12-14 10:29:02

标签: android xml layout navigation bottomnavigationview

我得到了什么:

enter image description here

我的目标是:

enter image description here

我将第一个添加到我的build.gralde:

compile 'com.android.support:design:25.3.1'
compile 'com.github.ittianyu:BottomNavigationViewEx:1.2.4'

然后这是我的布局xml:

<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
            android:id="@+id/bottomView"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_gravity="bottom"
            app:menu="@menu/bottom_nav_items"
            android:background="#f8f8f8"/>

1 个答案:

答案 0 :(得分:2)

为BottomNavigationView使用自定义布局。您的布局的可点击父级应包含属性 android:background =&#34;?selectableItemBackground&#34; 。 以下是一个例子。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:clickable="true"
android:duplicateParentState="true"
android:gravity="center"
android:orientation="vertical">

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />


<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sel" />

这将提供kitkat以上和kitkat上方的普通矩形的矩形波纹体验。如果你根本不想要涟漪(在所有Android版本中)。使用选择器作为背景。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape android:shape="rectangle">
        <solid android:color="#4C000000" />
    </shape>
</item>
<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/transparent" />
    </shape>
</item>

改为使用它。

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomNavigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:itemBackground="?selectableItemBackground"
    />