国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

頭部導航器組件的封裝

2019-11-09 16:37:57
字體:
來源:轉載
供稿:網友

頭部導航器組件的封裝

做移動開發的人員都知道,導航器是一個App中必不可少的組成部分,它可以實現頁面之間的切換。 本文所要講述的是如何去封裝一個頭部導航器組件,開發人員只需要傳入參數,就可以實現自己想要的導航器樣式。

具體代碼:

/** * NavigatorBar公共組件 * Create by zhoujinlong on 2017/1/5 */import React, {Component} from 'react';import { View, Text, StyleSheet, PixelRatio, Dimensions, Platform, TouchableOpacity,} from 'react-native';/** * _backgroundColor //背景顏色 * title1 //標題1【必須】 * title2 //標題2【必須】 * titleTextSize //標題字體大小 * firstTitleFunc //標題1按鈕回調方法 * secondTitleFunc //標題2按鈕回調方法 * titleColor //標題文字顏色 * leftIcon //左邊圖標 * leftIconColor //左邊圖標顏色 * leftText //左邊文字,如果圖像和圖標一起傳,則默認使用圖標 * leftTextColor //左邊文字顏色 * leftTextSize //左邊字體大小 * leftFunc //左邊欄的方法 * rightIcon //右邊圖標 傳具體的圖標 * rightIconColor //右邊圖標顏色 * rightText //右邊文字 * rightTextColor //右邊文字顏色 * rightTextSize //右邊字體大小 * rightFunc //右邊欄方法 * * 示例: * 導航欄左邊文字返回:<NavigatorBar title="工作" leftText="返回" leftFunc={()=>{alert(2);}} /> * 導航欄左邊圖片:<NavigatorBar title="工作" leftIcon={"&#xe7c0;"} leftFunc={()=>{alert(2);}} /> * */export default class NavigatorBar extends Component { constructor(PRops) { super(props); this.state = { checkedTitleStyle : [], uncheckedTitleStyle : [], firstTitleStyle:[], secondTitleStyle:[], params : props, } //選中的標題按鈕樣式處理 this.state.checkedTitleStyle.push(styles.checkedTitleStyle); props.titleColor ? this.state.checkedTitleStyle.push({color: props.titleColor}) : this.state.checkedTitleStyle.push({color: '#000000'}); props.titleTextSize ? this.state.checkedTitleStyle.push({fontSize:props.titleTextSize}):this.state.checkedTitleStyle.push({fontSize:18}); //未選中的標題按鈕樣式處理 this.state.uncheckedTitleStyle.push(styles.uncheckedTitleStyle); props.titleColor ? this.state.uncheckedTitleStyle.push({color: props.titleColor}) : this.state.uncheckedTitleStyle.push({color: '#000000'}); props.titleTextSize ? this.state.uncheckedTitleStyle.push({fontSize: props.titleTextSize}) : this.state.uncheckedTitleStyle.push({fontSize:18}); //初始化標題按鈕的樣式 this.state.firstTitleStyle = this.state.checkedTitleStyle; this.state.secondTitleStyle = this.state.uncheckedTitleStyle; } //標題1按鈕事件 firstTitlePress(){ if(this.state.params.secondTitleFunc===undefined){ alert("標題按鈕1沒有回調函數!"); }else{ this.state.params.firstTitleFunc(); } this.setState({ firstTitleStyle:this.state.checkedTitleStyle, secondTitleStyle:this.state.uncheckedTitleStyle, }); } //標題2按鈕事件2 secondTitlePress(){ if(this.state.params.secondTitleFunc===undefined){ alert("標題按鈕2沒有回調函數!"); }else{ this.state.params.secondTitleFunc(); } this.setState({ firstTitleStyle:this.state.uncheckedTitleStyle, secondTitleStyle:this.state.checkedTitleStyle, }); } render() { let {_backgroundColor,title1,title2,titleTextSize, titleColor, leftIcon, leftIconColor, leftText, leftTextSize, leftTextColor, leftFunc, rightIcon, rightIconColor, rightText, rightTextSize, rightTextColor, rightFunc} = this.state.params; let titleFalg = title1 || title2; let _titleFalg = title1 && title2; let backgroundStyle = []; //背景處理 backgroundStyle.push(styles.container); _backgroundColor?backgroundStyle.push({backgroundColor:_backgroundColor}):backgroundStyle.push({backgroundColor:"#CC1B23"}); //左邊欄處理 let leftFalg = leftIcon || leftText; if (leftFalg) { if (!leftFunc) { throw new Error("請傳左邊欄的回調方法"); } } let leftTextStyleArray = []; leftTextStyleArray.push(styles.leftTextStyle); leftTextColor ? leftTextStyleArray.push({color: leftTextColor}) : leftTextStyleArray.push({color: "#FFFFFF"}); leftTextSize ? leftTextStyleArray.push({fontSize: leftTextSize}) : leftTextStyleArray.push({fontSize:14}); //左邊圖標處理 let leftIconStyleArray = []; leftIconStyleArray.push(styles.leftIconStyle); leftIconColor ? leftIconStyleArray.push({color:leftIconColor}):leftIconStyleArray.push({color:"#FFFFFF"}); //右邊欄處理 let rightFalg = rightIcon || rightText; if (rightFalg) { if (!rightFunc) { throw new Error("請傳右邊欄的回調方法"); } } let rightTextStyleArray = []; rightTextStyleArray.push(styles.rightTextStyle); rightTextColor ? rightTextStyleArray.push({color: rightTextColor}) : rightTextStyleArray.push({color: "#FFFFFF"}); rightTextSize ? rightTextStyleArray.push({fontSize: rightTextSize}) : rightTextStyleArray.push({fontSize: 14}); //右邊圖標處理 let rightIconStyleArray = []; rightIconStyleArray.push(styles.rightIconStyle); rightIconColor ? rightIconStyleArray.push({color:rightIconColor}):rightIconStyleArray.push({color:"#FFFFFF"}); return ( <View style={backgroundStyle}> {//中間標題 titleFalg? _titleFalg? <View style={styles.titleStyle}> <TouchableOpacity activeOpacity={1} onPress={this.firstTitlePress.bind(this)}> <Text style={this.state.firstTitleStyle}>&nbsp;&nbsp;&nbsp;{title1}&nbsp;&nbsp;&nbsp;</Text> </TouchableOpacity> <TouchableOpacity activeOpacity={1} onPress={this.secondTitlePress.bind(this)}> <Text style={this.state.secondTitleStyle}>&nbsp;&nbsp;&nbsp;{title2}&nbsp;&nbsp;&nbsp;</Text> </TouchableOpacity> </View> : <View > { title1? <TouchableOpacity activeOpacity={1} onPress={this.firstTitlePress.bind(this)}> <Text style={[this.state.firstTitleStyle,{backgroundColor:_backgroundColor}]}>{title1}</Text> </TouchableOpacity> : <TouchableOpacity activeOpacity={1} onPress={this.firstTitlePress.bind(this)}> <Text style={this.state.secondTitleStyle}>{title2}</Text> </TouchableOpacity> } </View> :null } { //左邊欄 leftFalg ? <TouchableOpacity onPress={leftFunc} style={[styles.leftStyle]}> { leftText? leftIcon? <View style={styles.leftTouchStyle}> <Text style={leftIconStyleArray}>{leftIcon}</Text> <Text style={leftTextStyleArray}>{leftText}</Text> </View> : <View style={styles.leftTouchStyle}> <Text style={leftTextStyleArray}>{leftText}</Text> </View> : <View style={styles.leftTouchStyle}> <Text style={{fontFamily:'iconfont',color:"white",marginLeft:0, }}>{leftIcon}</Text> </View> } </TouchableOpacity> : null } {//右邊欄 rightFalg ? <TouchableOpacity onPress={rightFunc} style={styles.rightStyle}> { rightText? rightIcon? <View style={styles.rightTouchStyle}> <Text style={rightIconStyleArray}>{rightIcon}</Text> <Text style={rightTextStyleArray}>{rightText}</Text> </View> : <View style={styles.rightTouchStyle}> <Text style={rightTextStyleArray}>{rightText}</Text> </View> : <View style={styles.rightTouchStyle}> <Text style={{fontFamily:'iconfont',color:"white" }}>{rightIcon}</Text> </View> } </TouchableOpacity> :null } </View> ); }}const styles = StyleSheet.create({ container: { flexDirection: 'row', width: Dimensions.get('window').width, height: Platform.OS === 'ios' ? 64 : 44, justifyContent: 'center', alignItems: 'center', paddingTop: Platform.OS === 'ios' ? 20 : 0, }, titleStyle:{ flexDirection:'row', borderWidth:1, borderColor:"#FFFFFF", borderRadius:3, }, checkedTitleStyle:{ color:"blue", backgroundColor:"white", }, uncheckedTitleStyle:{ color:"blue", backgroundColor:"#CC1B23", borderRadius:3, }, leftStyle: { height: 44, position: 'absolute',//絕對布局 top: Platform.OS === 'ios' ? 20 : 0, left: 10, alignItems: 'center', }, leftTextStyle: { justifyContent: 'center', alignItems: 'flex-start', marginLeft: 0, }, leftIconStyle: { fontFamily:'iconfont', }, leftTouchStyle: { alignItems: 'center', height: 44, justifyContent: 'center', }, rightStyle: { height: 44, position: 'absolute',//絕對布局 top: Platform.OS === 'ios' ? 20 : 0, right:10, alignItems: 'center', }, rightTextStyle: { justifyContent: 'center', alignItems: 'flex-start', paddingRight:0, }, rightTouchStyle:{ alignItems: 'center', height: 44, justifyContent: 'center', }, rightIconStyle: { fontFamily:'iconfont', },});
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鸡泽县| 五台县| 自治县| 阜阳市| 兰溪市| 恩施市| 札达县| 阳谷县| 延川县| 马鞍山市| 东乌| 萍乡市| 乃东县| 涪陵区| 建德市| 河北区| 双鸭山市| 宁阳县| 绍兴市| 金川县| 崇阳县| 扎赉特旗| 称多县| 平顺县| 大方县| 镇原县| 衡水市| 云霄县| 平塘县| 咸宁市| 开原市| 永泰县| 手游| 陆川县| 金阳县| 怀仁县| 谢通门县| 专栏| 桂林市| 阜南县| 盘山县|