如何在 android BottomNavigationView 上获得方形效果而不是涟漪效果?
How to get square effect rather than ripple on android BottomNavigationView?
我得到的:
我的目标是:
我通过将此添加到我的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"/>
为 BottomNavigationView 使用自定义布局。并且您的布局的可点击父级应包含属性 android:background="?selectableItemBackground" 。
下面是一个例子。
<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 上方的矩形 Ripple 和 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"
/>
我得到的:
我的目标是:
我通过将此添加到我的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"/>
为 BottomNavigationView 使用自定义布局。并且您的布局的可点击父级应包含属性 android:background="?selectableItemBackground" 。 下面是一个例子。
<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 上方的矩形 Ripple 和 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"
/>