我对React-Native还是陌生的,到目前为止,我一直很喜欢。我正在尝试创建一个屏幕(用于跨平台应用程序),该屏幕的右上角有一个菜单图标,当我单击该菜单时,我想打开一个菜单,希望使用react-native-menu菜单显示“ Sign Out”和“ Account”菜单选项。此后很难弄清楚如何调用菜单。感谢任何帮助。
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
View,
} from 'react-native';
import ActionBar from 'react-native-action-bar';
export test class Main extends Component {
render() {
return (
<View style={styles.screen}>
<ActionBar
containerStyle={styles.bar}
backgroundColor='#33cc33'
rightIcons={[
{
name: 'menu',
onPress: () => console.log('menu clicked'),
},
]}
/>
</View>
);
}
}
const styles = StyleSheet.create({
screen: {
backgroundColor: '#33cc33',
flex: 1,
paddingTop: 10,
alignItems: 'center',
//padding: 10
},
});
AppRegistry.registerComponent('Main', () => Main);
Run Code Online (Sandbox Code Playgroud)
我尝试完成您的情况,我添加了react-native-drawer-layout用于创建菜单抽屉布局的库。您可以在此找到要安装的文件。
第1步 -创建菜单列表(我创建了一个单独的菜单列表,以便在添加其他菜单时更加轻松),它仅包含ArrayList。我叫那个文件Constants,你可以这样写Constants.js:
export const MENU_LIST = [
{ index: 1, name: 'Action' },
{ index: 2, name: 'Sign Out' },
]Run Code Online (Sandbox Code Playgroud)
第2步 -创建菜单组件以显示菜单列表。在Menu.js你这样写:
import React, { Component } from 'react';
import { View, ScrollView, Text, TouchableOpacity } from 'react-native';
const menuList = require('./Constants.js');
export default class Menu extends Component {
render() {
return (
<View style={{ flex:1, backgroundColor: '#33cc33'}}>
<ScrollView>
{menuList.MENU_LIST.map(item => (
<TouchableOpacity
key={item.index}
onPress={() => console.log('entered menu')}
>
<Text style={{color: 'white', fontSize: 16, paddingLeft: 20, paddingTop: 16}}>{item.name}</Text>
</TouchableOpacity>
))}
</ScrollView>
</View>
);
}
}Run Code Online (Sandbox Code Playgroud)
步骤3-重构主要组件,例如:
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, View } from 'react-native';
import ActionBar from 'react-native-action-bar';
import DrawerLayout from 'react-native-drawer-layout';
import Menu from './Menu';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
drawerClosed: true,
};
this.toggleDrawer = this.toggleDrawer.bind(this);
this.setDrawerState = this.setDrawerState.bind(this);
}
setDrawerState() {
this.setState({
drawerClosed: !this.state.drawerClosed,
});
}
toggleDrawer = () => {
if (this.state.drawerClosed) {
this.DRAWER.openDrawer();
} else {
this.DRAWER.closeDrawer();
}
}
render() {
return (
<DrawerLayout
drawerWidth={300}
ref={drawerElement => {
this.DRAWER = drawerElement;
}}
drawerPosition={DrawerLayout.positions.left}
onDrawerOpen={this.setDrawerState}
onDrawerClose={this.setDrawerState}
renderNavigationView={() => <Menu />}
>
<ActionBar
containerStyle={styles.bar}
backgroundColor="#33cc33"
leftIconName={'menu'}
onLeftPress={this.toggleDrawer}/>
</DrawerLayout>
);
}
}
const styles = StyleSheet.create({
screen: {
backgroundColor: '#33cc33',
flex: 1,
paddingTop: 10,
alignItems: 'center',
//padding: 10
},
});
AppRegistry.registerComponent('Main', () => App);Run Code Online (Sandbox Code Playgroud)
在我的模拟器中,它将显示为:
当我点击菜单图标时,它将显示为:
UPDATE-1:
如果要使组件抽屉菜单不填满底部,可以在component中播放样式<Menu />,我给包装器留出一定的边距,例如:
const styles = StyleSheet.create({
wrapper: {
backgroundColor: '#33cc33',
marginTop: 50,
},
listMenu: {
color: 'white',
fontSize: 16,
paddingLeft: 20,
paddingTop: 12,
paddingBottom: 12,
}
});Run Code Online (Sandbox Code Playgroud)
并在组件中添加样式,<Menu />例如:
export default class Menu extends Component {
render() {
return (
<View style={styles.wrapper}> //add style wrapper
<ScrollView>
{menuList.MENU_LIST.map(item => (
<TouchableOpacity
key={item.index}
onPress={() => console.log('entered menu')}
>
<Text style={styles.listMenu}>{item.name}</Text> //add style menu
</TouchableOpacity>
))}
</ScrollView>
</View>
);
}
}Run Code Online (Sandbox Code Playgroud)
完整代码Menu.js如:
import React, { Component, PropTypes } from 'react';
import { View, ScrollView, Text, TouchableOpacity, Image, StyleSheet } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
const menuList = require('./Constants.js');
export default class Menu extends Component {
render() {
return (
<View style={styles.wrapper}>
<ScrollView>
{menuList.MENU_LIST.map(item => (
<TouchableOpacity
key={item.index}
onPress={() => console.log('entered menu')}
>
<Text style={styles.listMenu}>{item.name}</Text>
</TouchableOpacity>
))}
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
wrapper: {
backgroundColor: '#33cc33',
marginTop: 50,
},
listMenu: {
color: 'white',
fontSize: 16,
paddingLeft: 20,
paddingTop: 12,
paddingBottom: 12,
}
});Run Code Online (Sandbox Code Playgroud)
结果像:
UPDATE-2:
如果您想menu在右侧更改位置,则根据您在注释中的情况而定。您必须先更改抽屉的位置。
实际上:
main文件中看到类似: render() {
return (
<DrawerLayout
/* This for set width drawer */
drawerWidth={300}
/* end */
ref={drawerElement => {
this.DRAWER = drawerElement;
}}
/* This for set position drawer */
drawerPosition={DrawerLayout.positions.left}
/* end */
onDrawerOpen={this.setDrawerState}
onDrawerClose={this.setDrawerState}
renderNavigationView={() => <Menu />}
>
<ActionBar
containerStyle={styles.bar}
backgroundColor="#33cc33"
leftIconName={'menu'}
onLeftPress={this.toggleDrawer}
/>
</DrawerLayout>
);
}Run Code Online (Sandbox Code Playgroud)
希望:
render() {
return (
<DrawerLayout
drawerWidth={300}
ref={drawerElement => {
this.DRAWER = drawerElement;
}}
// i change the position to the right.
drawerPosition={DrawerLayout.positions.Right}
onDrawerOpen={this.setDrawerState}
onDrawerClose={this.setDrawerState}
renderNavigationView={() => <Menu />}
>
<ActionBar
containerStyle={styles.bar}
backgroundColor="#33cc33"
rightIcons={[
{
name: 'menu',
onPress: this.toggleDrawer,
},
]}
/>
</DrawerLayout>
);
}Run Code Online (Sandbox Code Playgroud)
如果您想了解Android上的DrawerLayout,可以阅读文档。
对于这种情况,我的模拟器显示如下:
希望我的回答能对您有所帮助,并给您另一个开发应用程序的想法。战斗...;))
| 归档时间: |
|
| 查看次数: |
9845 次 |
| 最近记录: |