插件窝 干货文章 底部导航栏中间凸起效果如何实现?

底部导航栏中间凸起效果如何实现?

Android bottom layout center 882    来源:    2025-03-25

实现底部导航栏中间凸起效果的方法

要实现底部导航栏中间凸起的效果,可以通过以下几种方式实现:

1. 使用自定义布局和形状

Android实现方案

<!-- 在布局文件中 -->
<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>

2. 使用FloatingActionButton实现凸起效果

<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>

3. CSS实现方案(Web端)

<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);
}

4. Flutter实现方案

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),
),

注意事项

  1. 确保凸起部分不会遮挡重要内容
  2. 考虑不同屏幕尺寸的适配
  3. 为凸起按钮添加适当的阴影和点击效果
  4. 在iOS上遵循HIG设计规范,Android上遵循Material Design规范

以上方案可以根据具体平台和需求进行调整和组合使用。