如何使用DrawerLayoutAndroid组件在react-native应用程序中构建抽屉?
作者:互联网
嘿家伙我是新来回应当地尝试建立汉堡包抽屉协助导航,但无法找到适当的文件.我发现了一个,但它是用redux,所以我放手了.任何人都可以指导我如何为react-native应用程序创建drawerlayout.
提前致谢.
解决方法:
您可以从以下代码示例中获取帮助:
您的toolbar.js可以是:
'use strict';
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text
} from 'react-native';
var ToolbarAndroid = require('ToolbarAndroid');
class MyToolbar extends Component {
render() {
var navigator = this.props.navigator;
return (
<ToolbarAndroid
title={this.props.title}
navIcon={require('./icons/ic_menu_white_24dp.png')}
style = {styles.toolbar}
titleColor={'white'}
onIconClicked={this.props.sidebarRef}/>
);
}
const styles = StyleSheet.create({
//define your own style
}
});
module.exports = MyToolbar;
并在实施抽屉:
'use strict';
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
Navigator,
TouchableHighlight,
TouchableOpacity,
DrawerLayoutAndroid,
ScrollView,
} from 'react-native';
var ToolbarAndroid = require('ToolbarAndroid');
var MyToolbar = require('./MyToolbar');
var MenuItem = require('./MenuItem');
class OpenDraweFromToolbar extends Component {
render() {
var navigationView = (
<ScrollView>
<View style = {{height:100, backgroundColor:'blue', justifyContent:'center'}}>
<Text style = {{height:25, color:'white', fontSize:25, marginLeft:20}}>Welcome To ReactNative</Text>
</View>
<ListView
//render your list items
</ScrollView>
);
return (
<DrawerLayoutAndroid
drawerWidth={300}
drawerPosition={DrawerLayoutAndroid.positions.Left}
renderNavigationView={() => navigationView}
ref={'DRAWER'}>
<MyToolbar style={styles.toolbar}
title={'Calendar'}
navigator={this.props.navigator}
{sidebarRef={()=>this._setDrawer()}}/>
<View style={{flex: 1, alignItems: 'center'}}>
<Text style={{margin: 10, fontSize: 15, textAlign: 'right'}}>Hello</Text>
</View>
</DrawerLayoutAndroid>
);
}
_setDrawer() {
this.refs['DRAWER'].openDrawer();
}
}
const styles = StyleSheet.create({
//your own style implementation
});
module.exports = OpenDraweFromToolbar;
您应该将drawerlayout打开方法作为props传递给工具栏,并在工具栏组件中将sidebarRef称为props,它会自动调用以前的OpenDraweFromToolbar.js的drawerlayout开放方法.
我希望你能从上面的例子中获得帮助.快乐的编码!
标签:android,navigation-drawer,menu,react-native,hamburger-menu 来源: https://codeday.me/bug/20190527/1166374.html