React native 反应本机操作栏和反应本机菜单
到目前为止,我还没有反应过来,喜欢它。我正在尝试创建一个屏幕(用于跨平台应用程序),屏幕右上角有一个菜单图标,单击后,我想打开一个菜单,希望使用react native菜单显示“注销”和“帐户”菜单选项。在这之后很难弄清楚如何调用菜单。谢谢你的帮助React native 反应本机操作栏和反应本机菜单,react-native,React Native,到目前为止,我还没有反应过来,喜欢它。我正在尝试创建一个屏幕(用于跨平台应用程序),屏幕右上角有一个菜单图标,单击后,我想打开一个菜单,希望使用react native菜单显示“注销”和“帐户”菜单选项。在这之后很难弄清楚如何调用菜单。谢谢你的帮助 import React, { Component } from 'react'; import { AppRegistry, StyleSheet, View, } from 'react-nati
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);
import React,{Component}来自'React';
进口{
评估学,
样式表,
看法
}从“反应本机”;
从“反应本机操作栏”导入操作栏;
导出测试类主扩展组件{
render(){
返回(
console.log('单击菜单'),
},
]}
/>
);
}
}
const styles=StyleSheet.create({
屏幕:{
背景颜色:“#33cc33”,
弹性:1,
paddingTop:10,
对齐项目:“居中”,
//填充:10
},
});
AppRegistry.registerComponent('Main',()=>Main);
我使用本机库创建菜单,这是文档。您可以尝试搜索所需的组件
这是我尝试制作菜单的一个例子
/**
*示例React本机应用程序
*
*@flow
*/
import React,{Component}来自'React';
从“react native”导入{AppRegistry};
从“本机基础”导入{容器、内容、标题、正文、右侧、按钮、图标、标题、抽屉、文本};
类边栏扩展组件{
render(){
返回(
账户
签到
)
}
}
导出默认类应用程序扩展组件{
closeDrawer=()=>{
此.drawer.\u root.close()
}
openDrawer=()=>{
此.drawer.\u root.open()
}
render(){
返回(
{this.drawer=ref;}}
内容={}
onClose={()=>this.closeDrawer()}>
标题
)
}
}
AppRegistry.registerComponent('Main',()=>App);
你可以自己设计菜单。也许它可以帮助您,谢谢:)我尝试完成您的案例,我添加了库react native drawer layout
以创建菜单抽屉布局。您可以在中找到以进行安装
第1步-创建菜单列表(我创建了一个单独的列表,以便在我想添加另一个菜单时更容易),它是只包含内容的ArrayList。我调用了该文件Constants
,您可以在Constants.js
中编写如下内容:
导出常量菜单\u列表=[
{索引:1,名称:'Action'},
{索引:2,名称:'注销'},
]
实现这些事情
+import ModalDropdown from 'react-native-modal-dropdown';
class FooBar extends PureComponent {
constructor(props) {
super(props);
+ this.dropdownOptions = [{
+ text: 'Scan',
+ icon: require('../images/scan.png'),
+ onPress: this.toScan,
+ }, {
+ text: 'Share',
+ icon: require('../images/share.png'),
+ onPress: this.toShare,
+ }];
+ adjustDropdownStyle = style => {
+ return {
+ width: 110,
+ height: 96, // calculated from margin and height of renderDropdownItem bellow
+ right: 0,
+ top: style.top,
+ };
+ }
+
+ renderDropdownItem = (item, index, highlighted) => {
+ return (
+ <View style={{alignItems: 'center', flexDirection: 'row'}}>
+ <Image source={item.icon} style={{margin: 10, width: 28, height: 28 }}/>
+ <Text style={{fontSize: 15}}>
+ {item.text}
+ </Text>
+ </View>
+ );
+ }
+
+ onDropdownSelect = (index, item) => item.onPress()
+
render() {
let navs = {
Center: {
text: 'Home',
},
Right: {
image: require('../images/more.png'),
+ onPress: () => this.dropdown && this.dropdown.show(),
},
};
<Header navs={navs}/>
+ <ModalDropdown
+ ref={view => {this.dropdown = view;}}
+ style={{height: 0}}
+ adjustFrame={this.adjustDropdownStyle}
+ options={this.dropdownOptions}
+ renderRow={this.renderDropdownItem.bind(this)}
+ onSelect={this.onDropdownSelect.bind(this)}
+ />
+从“反应本机模态下拉列表”导入模态下拉列表;
类FooBar扩展了PureComponent{
建造师(道具){
超级(道具);
+this.dropdownpoptions=[{
+文本:“扫描”,
+图标:需要('../images/scan.png'),
+onPress:这是toScan,
+ }, {
+文本:“共享”,
+图标:需要('../images/share.png'),
+onPress:这是toShare,
+ }];
+adjustDropdownStyle=样式=>{
+返回{
+宽度:110,
+高度:96,//根据以下renderDropdownItem的边距和高度计算
+右:0,,
+top:style.top,
+ };
+ }
+
+renderDropdownItem=(项目、索引、高亮显示)=>{
+返回(
+
+
+
+{item.text}
+
+
+ );
+ }
+
+onDropdownSelect=(索引,项)=>item.onPress()
+
render(){
让导航={
中心:{
文本:“主页”,
},
对:{
image:require('../images/more.png'),
+onPress:()=>this.dropdown&&this.dropdown.show(),
},
};
+{this.dropdown=view;}}
+样式={{高度:0}}
+adjustFrame={this.adjustDropdownStyle}
+选项={this.dropdownpoptions}
+renderRow={this.renderDropdownItem.bind(this)}
+onSelect={this.onDropdownSelect.bind(this)}
+ />
您好,我使用的是ReactNative
上的创建组件菜单。也许您也可以试试。非常感谢。很好的例子!因为抽屉从上到下都是满的,看起来不像菜单,所以是否可以使用react native菜单,它看起来像一个很好的菜单。感谢您的帮助。这是可能的…但是使用react native drawer layout
也可以。您只需为组件抽屉菜单添加样式。您可以在我的回答中看到,我更新了它。再次感谢。但是为什么我不能将“菜单”设置在右侧?当我更改为右侧时,它将消失。对于菜单,我可以使用TouchableHighlight并仍然从常量中提取文本吗?yaapz..因为您必须设置抽屉。对于第一种情况,我将其设置为s的一半屏幕和左侧的位置。如果您想更改为右侧,可以将其设置为右侧位置。我将更新我的答案以向您显示react native drawer layout
用于创建菜单选项。;)这看起来很棒!感谢您的帮助!是否可以在菜单中显示图标而不是文本?如果图标点击了吗?
+import ModalDropdown from 'react-native-modal-dropdown';
class FooBar extends PureComponent {
constructor(props) {
super(props);
+ this.dropdownOptions = [{
+ text: 'Scan',
+ icon: require('../images/scan.png'),
+ onPress: this.toScan,
+ }, {
+ text: 'Share',
+ icon: require('../images/share.png'),
+ onPress: this.toShare,
+ }];
+ adjustDropdownStyle = style => {
+ return {
+ width: 110,
+ height: 96, // calculated from margin and height of renderDropdownItem bellow
+ right: 0,
+ top: style.top,
+ };
+ }
+
+ renderDropdownItem = (item, index, highlighted) => {
+ return (
+ <View style={{alignItems: 'center', flexDirection: 'row'}}>
+ <Image source={item.icon} style={{margin: 10, width: 28, height: 28 }}/>
+ <Text style={{fontSize: 15}}>
+ {item.text}
+ </Text>
+ </View>
+ );
+ }
+
+ onDropdownSelect = (index, item) => item.onPress()
+
render() {
let navs = {
Center: {
text: 'Home',
},
Right: {
image: require('../images/more.png'),
+ onPress: () => this.dropdown && this.dropdown.show(),
},
};
<Header navs={navs}/>
+ <ModalDropdown
+ ref={view => {this.dropdown = view;}}
+ style={{height: 0}}
+ adjustFrame={this.adjustDropdownStyle}
+ options={this.dropdownOptions}
+ renderRow={this.renderDropdownItem.bind(this)}
+ onSelect={this.onDropdownSelect.bind(this)}
+ />