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

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

凸起 BottomNavigation Android FloatingActionBu 170    来源:    2025-03-24

实现底部导航栏的中间凸起效果通常可以通过以下几种方式来实现,具体取决于你使用的技术栈(如原生Android、iOS、Flutter、React Native等)。以下是几种常见的方法:

1. 原生Android实现

在Android中,你可以使用BottomNavigationView并结合自定义布局来实现中间凸起的效果。

步骤:

  1. 自定义布局:创建一个自定义的BottomNavigationView布局,并在中间位置添加一个凸起的按钮。
  2. 使用CoordinatorLayout:将BottomNavigationView放在CoordinatorLayout中,并使用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"
        app:menu="@menu/bottom_navigation_menu" />

    <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="16dp"
        android:src="@drawable/ic_add"
        app:elevation="8dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

2. 原生iOS实现

在iOS中,你可以使用UITabBarController并结合自定义的UITabBar来实现中间凸起的效果。

步骤:

  1. 自定义UITabBar:创建一个自定义的UITabBar,并在中间位置添加一个凸起的按钮。
  2. 使用UIView:在UITabBar上添加一个UIView来实现凸起效果。
class CustomTabBar: UITabBar {
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var sizeThatFits = super.sizeThatFits(size)
        sizeThatFits.height = 80 // 增加高度以容纳凸起按钮
        return sizeThatFits
    }
}

class CustomTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let customTabBar = CustomTabBar()
        self.setValue(customTabBar, forKey: "tabBar")

        let middleButton = UIButton(type: .custom)
        middleButton.frame = CGRect(x: (self.view.bounds.width / 2) - 30, y: -20, width: 60, height: 60)
        middleButton.setImage(UIImage(named: "middle_button"), for: .normal)
        middleButton.addTarget(self, action: #selector(middleButtonAction), for: .touchUpInside)
        customTabBar.addSubview(middleButton)
    }

    @objc func middleButtonAction() {
        // 处理中间按钮点击事件
    }
}

3. Flutter实现

在Flutter中,你可以使用BottomNavigationBar并结合FloatingActionButton来实现中间凸起的效果。

步骤:

  1. 使用Scaffold:在Scaffold中使用BottomNavigationBarFloatingActionButton
  2. 调整FloatingActionButton位置:通过FloatingActionButtonLocation来调整按钮的位置。
Scaffold(
  appBar: AppBar(
    title: Text('Bottom Navigation with FAB'),
  ),
  body: Center(
    child: Text('Home'),
  ),
  bottomNavigationBar: BottomNavigationBar(
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        label: 'Home',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.business),
        label: 'Business',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.school),
        label: 'School',
      ),
    ],
    currentIndex: _selectedIndex,
    selectedItemColor: Colors.amber[800],
    onTap: _onItemTapped,
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: () {
      // 处理中间按钮点击事件
    },
    child: Icon(Icons.add),
  ),
  floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);

4. React Native实现

在React Native中,你可以使用react-navigation并结合自定义组件来实现中间凸起的效果。

步骤:

  1. 使用react-navigation:设置TabNavigator
  2. 自定义中间按钮:在TabNavigator中添加一个自定义的中间按钮。
import React from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer } from '@react-navigation/native';

function HomeScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Home!</Text>
    </View>
  );
}

function SettingsScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Settings!</Text>
    </View>
  );
}

const Tab = createBottomTabNavigator();

function MyTabs() {
  return (
    <Tab.Navigator
      tabBarOptions={{
        style: styles.tabBar,
      }}
    >
      <Tab.Screen name="Home" component={HomeScreen} />
      <Tab.Screen
        name="Middle"
        component={HomeScreen}
        options={{
          tabBarButton: (props) => (
            <TouchableOpacity
              style={styles.middleButton}
              onPress={() => {
                // 处理中间按钮点击事件
              }}
            >
              <Text style={styles.middleButtonText}>+</Text>
            </TouchableOpacity>
          ),
        }}
      />
      <Tab.Screen name="Settings" component={SettingsScreen} />
    </Tab.Navigator>
  );
}

const styles = StyleSheet.create({
  tabBar: {
    position: 'absolute',
    bottom: 25,
    left: 20,
    right: 20,
    borderRadius: 15,
    height: 90,
  },
  middleButton: {
    top: -30,
    justifyContent: 'center',
    alignItems: 'center',
    width: 60,
    height: 60,
    borderRadius: 30,
    backgroundColor: '#FF6347',
  },
  middleButtonText: {
    fontSize: 24,
    color: '#FFF',
  },
});

export default function App() {
  return (
    <NavigationContainer>
      <MyTabs />
    </NavigationContainer>
  );
}

总结

无论你使用哪种技术栈,实现底部导航栏的中间凸起效果的核心思路都是通过自定义布局或组件,并在中间位置添加一个凸起的按钮。你可以根据具体的需求和技术栈选择合适的方法来实现这一效果。