ReactNative学习八-搜索栏的基本布局

前端之家收集整理的这篇文章主要介绍了ReactNative学习八-搜索栏的基本布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.布局


2.代码如下


  1. /**
  2. * 扫码框
  3. */
  4. 'use strict';
  5.  
  6. import React,{
  7. Component,Image,TextInput,View,Platform,StyleSheet
  8. } from 'react-native';
  9.  
  10.  
  11. //export 因为要在其他类中使用
  12. export default class Header extends Component{
  13. render(){
  14. return (
  15. <View style={styles.container}>
  16.  
  17. <Image source={require('./images/header/header_logo.png')} style={styles.logo}/>
  18.  
  19. <View style={styles.searchBox}>
  20.  
  21. <Image source={require('./images/header/icon_search.png')} style={styles.searchIcon}/>
  22. <TextInput style={styles.inputText}
  23. keyboardType='web-search'
  24. placeholder='搜索京东商品/店铺' />
  25. <Image source={require('./images/header/icon_voice.png')} style={styles.voiceIcon}/>
  26. </View>
  27.  
  28. <Image source={require('./images/header/icon_qr.png')} style={styles.scanIcon}/>
  29.  
  30. </View>
  31. )
  32. }
  33. }
  34.  
  35. //样式
  36. const styles = StyleSheet.create({
  37. container: {
  38. flexDirection: 'row',// 水平排布
  39. paddingLeft: 10,paddingRight: 10,paddingTop: Platform.OS === 'ios' ? 20 : 0,// 处理iOS状态栏
  40. height: Platform.OS === 'ios' ? 68 : 48,// 处理iOS状态栏
  41. backgroundColor: '#d74047',alignItems: 'center' // 使元素垂直居中排布,当flexDirection为column时,为水平居中
  42. },logo: {//图片logo
  43. height: 24,width: 64,resizeMode: 'stretch' // 设置拉伸模式
  44. },searchBox:{//搜索
  45. height:30,flexDirection: 'row',// 水平排布
  46. flex:1,borderRadius: 5,// 设置圆角边
  47. backgroundColor: 'white',alignItems: 'center',marginLeft: 8,marginRight: 8,},searchIcon: {//搜索图标
  48. height: 20,width: 20,marginLeft: 5,resizeMode: 'stretch'
  49. },inputText:{
  50. flex:1,backgroundColor:'transparent',fontSize:15,voiceIcon: {
  51. marginLeft: 5,width: 15,height: 20,scanIcon: {//搜索图标
  52. height: 26.7,width: 26.7,});



3.注意事项
1.style的使用,当使用StyleSheet创建的样式时,外层只需要一层{},而直接声明需要再加一层,即直接声明了匿名变量
2.Image的source可以使用网络图片或本地资源,使用本地资源时,类似require.js的包引用,而使用网络资源时,使用方法如下:source={{uri:'http://xxxxxxx'}}
3.TextInput的键盘类型可以使用keyboardType进行设置,占位字符使用placeholder设置,具体请参见官方文档

猜你在找的React相关文章