要实现底部导航栏中间凸起的效果,可以通过以下几种方式实现:
<!-- 在布局文件中 -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:itemBackground="@drawable/bottom_nav_background"
app:itemIconTint="@drawable/bottom_nav_colors"
app:itemTextColor="@drawable/bottom_nav_colors"
app:menu="@menu/bottom_nav_menu" />
创建自定义形状的drawable (bottom_nav_background.xml
):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
</shape>
</item>
<item
android:width="60dp"
android:height="60dp"
android:gravity="center">
<shape android:shape="oval">
<solid android:color="@color/white"/>
<stroke android:width="1dp" android:color="@color/gray"/>
</shape>
</item>
</layer-list>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 其他内容 -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="20dp"
app:srcCompat="@drawable/ic_add" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<div class="bottom-nav">
<div class="nav-item">首页</div>
<div class="nav-item">搜索</div>
<div class="nav-item center-item">
<div class="center-button">+</div>
</div>
<div class="nav-item">消息</div>
<div class="nav-item">我的</div>
</div>
.bottom-nav {
display: flex;
position: fixed;
bottom: 0;
width: 100%;
height: 60px;
background: white;
box-shadow: 0 -2px 5px rgba(0,0,0,0.1);
justify-content: space-around;
align-items: center;
}
.nav-item {
flex: 1;
text-align: center;
}
.center-item {
position: relative;
}
.center-button {
position: absolute;
bottom: 15px;
left: 50%;
transform: translateX(-50%);
width: 50px;
height: 50px;
border-radius: 50%;
background: linear-gradient(to right, #ff5e62, #ff9966);
color: white;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
BottomAppBar(
shape: CircularNotchedRectangle(),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(icon: Icon(Icons.home), onPressed: () {}),
IconButton(icon: Icon(Icons.search), onPressed: () {}),
SizedBox(width: 48), // 为FAB留出空间
IconButton(icon: Icon(Icons.notifications), onPressed: () {}),
IconButton(icon: Icon(Icons.person), onPressed: () {}),
],
),
),
FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
以上方案可以根据具体平台和需求进行调整和组合使用。