我想在我的RN应用程序的抽屉导航底部添加注销按钮.

As you can see the Logout button is located at the bottom of the drawer menu. How can I move it to the bottom of the drawer panel?

我试图用以下方式使用contentComponent:

const DrawerWithLogoutButton = (props) => (
  <ScrollView>
    <SafeAreaView style={styles.container} forceInset={{ top: 'always', horizontal: 'never' }}>
      <DrawerItems {...props} />
    </SafeAreaView>
    <Button
      style={styles.logoutButton}
      title="Logout"
      onPress={() => props.navigation.navigate('Login') }/>
  </ScrollView>
);

export default Home = createDrawerNavigator({
  // screens
}, {
  // other settings
  contentComponent: DrawerWithLogoutButton,
});

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
  },
  logoutButton: {
    backgroundColor: 'red',
    position: 'absolute',
    bottom: 0
  }
});

结果,我在菜单底部有一个注销按钮.但我想把它放在抽屉面板的底部

此外,我希望注销按钮看起来像其他菜单项,并有一个图标

有没有一种方法可以创建一个抽屉导航器,它的菜单项没有屏幕,只是像我的例子中那样的注销操作?

推荐答案

我能够将抽屉菜单底部的注销与ScrollView容器中添加justifyContent: 'space-between'对齐.你可以在图中看到结果

the Logout button is located at the bottom of the drawer menu

结果源代码如下所示:

const DrawerWithLogoutButton = (props) => (
  <ScrollView contentContainerStyle={{flex: 1,  flexDirection: 'column', justifyContent: 'space-between' }}>
    <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
      <DrawerItems {...props} />
    </SafeAreaView>
    <TouchableOpacity>
      <View style={styles.item}>
        <View style={styles.iconContainer}>
          <Image source={require('./img/logout.png')} style={styles.icon}></Image>
        </View>
        <Text style={styles.label}>Logout</Text>
      </View>
    </TouchableOpacity>
  </ScrollView>
);

const styles = StyleSheet.create({
  item: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  label: {
    margin: 16,
    fontWeight: 'bold',
    color: 'rgba(0, 0, 0, .87)',
  },
  iconContainer: {
    marginHorizontal: 16,
    width: 24,
    alignItems: 'center',
  },
  icon: {
    width: 24,
    height: 24,
  }
});

React-native相关问答推荐

在Reaction Native With EXPO中播放来自OpenAI TTS API的音频响应

RTK 查询显示互联网何时关闭?

React Native - 如何在从另一个屏幕导航后将 ScrollView 滚动到给定位置

React Native Navigation const { navigate } = this.props.navigation;

如何在 React Native 中正确地将组件导入到我的导航器中?

React Native Ios错误 - ENOENT:no such file or directory, uv_cwd (null)

React Native 获取视图相对于屏幕的绝对位置

如何将抖动动画添加到视图(react-native)?

如何在 React Native 中删除警告

React native base headers for ios not found

我们如何将 Firebase Analytics 与基于 expo 的 react-native 应用程序一起使用

React native expection java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/{package}/lib-main/libgnustl_shared.so" is 32-bit instead of 64-bit

如何在我的 React Native Android 应用程序中显示当前的 CodePush 版本标签?

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.

在 React Native 中,如何使用浅渲染测试我的组件?

expo 已过期卸载并再次运行以升级

React Native onLayout 与 React Hooks

您将如何在 Alert 中模拟onPress?

如何在 React Native 中等待 Alert 对话框的响应?

React-Native 水平滚动视图分页:预览下一页/卡片