javascript - ToolBarAndroid 未在 React Native 中呈现

标签 javascript android react-native

我试图让一个基本的 Android 工具栏呈现在我的 React native View 上,但我遇到了麻烦。使用我当前的代码,它运行良好(没有错误或任何错误),但工具栏不存在。关于我做错了什么有什么建议吗?提前致谢!

    'use strict';
    const React = require('react-native');
    const MaterialKit = require('react-native-material-kit');
    const {
      AppRegistry,
      DrawerLayoutAndroid,
      StyleSheet,
      Text,
      View,
      ToolbarAndroid,
    } = React;

    const DRAWER_REF = 'drawer';

    const OpenLightApp = React.createClass({
      render: function() {
        const navigationView = (
          <View style={{flex: 1, backgroundColor: '#88D8EC'}}>
            <Text style={{margin: 10, fontSize: 15, textAlign: 'left'}}>Drawer Item</Text>
          </View>
        );

        return (

          <DrawerLayoutAndroid
            drawerWidth={300}
            ref={DRAWER_REF}
            drawerPosition={DrawerLayoutAndroid.positions.Left}
            renderNavigationView={() => navigationView}>
            <View style={styles.container}>
            <ToolbarAndroid
              navIcon={require('image!hamburger')}
              title="OpenLight"
              titleColor="black"
              style={styles.toolbar}
              onIconClicked={() => this.refs[DRAWER_REF].openDrawer()} />
              <Text style={styles.welcome}>
                Example Text
              </Text>
              <Text style={styles.instructions}>
                To get started, edit index.android.js
              </Text>
              <Text style={styles.instructions}>
                Shake or press menu button for dev menu
              </Text>
            </View>
          </DrawerLayoutAndroid>
        );
      }
    });

    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      },
      welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
      },
      instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
      },
      toolbar: {
        backgroundColor: '#00a2ed',
        height: 56,
      },
    });

    AppRegistry.registerComponent('OpenLightApp', () => OpenLightApp);

最佳答案

您需要为工具栏指定高度和宽度,否则它不会呈现。

您的工具栏代码应如下所示

          <ToolbarAndroid
            style={styles.toolbar}
            title="Movies"
            navIcon={require("../../ic_launcher.png")}
            onActionSelected={this.onActionSelected}
            titleColor= "000"
            actions = {[
              {title: "Log out", show: "never"}
            ]}
            />

选择 Action

     onActionSelected(position) {
     }

工具栏样式

 toolbar: {
   backgroundColor: '#2196F3',
   height: 56,
   alignSelf: 'stretch',
   textAlign: 'center',
 }, 

结果

enter image description here

关于javascript - ToolBarAndroid 未在 React Native 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34124400/

相关文章:

javascript - 我们可以绘制同时包含实线和虚线的折线图吗?

javascript - 无法使用 Google 距离矩阵读取未定义的属性 'text'

android - ConstratintLayout : How to center a view according to another view, 垂直

react-native - React Native Scrollview 与 flexGrow 不滚动

react-native - 如何检测 View 是否在 native 屏幕上可见

Javascript 将 div 从屏幕左侧滑出

javascript - 动态删除附件?

android - 从 SVG 设置自定义按钮选择器位图项

java - Android 下载管理器 - 无法知道下载的大小

javascript - React-Native: Release模式下的 JavaScript 正则表达式异常(无效组)