bottomnavigationview 中间凸起
时间: 2023-09-10 22:15:25 浏览: 142
要在 BottomNavigationView 中间凸起,可以使用第三方库来实现,比如使用 AHBottomNavigation 库。步骤如下:
1. 在 build.gradle 文件中添加依赖:
```groovy
dependencies {
implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'
}
```
2. 在布局文件中添加 AHBottomNavigation 控件:
```xml
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ah_bottom_navigation_background_color="@color/colorPrimary"
app:ah_bottom_navigation_title_text_color="@color/white"
app:ah_bottom_navigation_active_item_color="@android:color/white"
app:ah_bottom_navigation_inactive_item_color="@color/white"
app:ah_bottom_navigation_shadow="false"
app:ah_bottom_navigation_use_theme_colors="false"
app:ah_bottom_navigation_default_background_color="@color/colorPrimary"
app:ah_bottom_navigation_item_margin_top="8dp"
app:ah_bottom_navigation_selected_item="1"
app:ah_bottom_navigation_notification_background_color="@color/colorAccent"
app:ah_bottom_navigation_notification_margin_left="4dp"
app:ah_bottom_navigation_notification_margin_top="4dp"
app:ah_bottom_navigation_notification_text_size="10sp" />
```
3. 在代码中设置 BottomNavigationView:
```java
AHBottomNavigation bottomNavigationView = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
// 添加导航项
AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.tab_1, R.drawable.ic_home, R.color.color_tab_1);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.tab_2, R.drawable.ic_search, R.color.color_tab_2);
AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.tab_3, R.drawable.ic_profile, R.color.color_tab_3);
bottomNavigationView.addItem(item1);
bottomNavigationView.addItem(item2);
bottomNavigationView.addItem(item3);
// 设置中间凸起
bottomNavigationView.setBehaviorTranslationEnabled(false);
AHBottomNavigationItem item = new AHBottomNavigationItem("", R.drawable.ic_add, R.color.color_tab_add);
bottomNavigationView.addItem(item);
bottomNavigationView.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
```
注意,AHBottomNavigation 库中的 BottomNavigationView 并非是 Android 原生的 BottomNavigationView,因此在使用时需要注意一些细节。
阅读全文