詳細講一下在RN(ReactNative)中,6個比較常用的組件以及詳細的用法

1.View 组件(基础容器)

javascript 复制代码
import { View, StyleSheet } from 'react-native';

const MyComponent = () => {
  return (
    <View 
      style={styles.container}  // 样式
      pointerEvents="none"      // 控制触摸事件:'none', 'box-none', 'box-only', 'auto'
      accessible={true}         // 无障碍访问
    >
      {/* 子组件 */}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,                    // flex 布局
    padding: 10,                // 内边距
    backgroundColor: '#fff',    // 背景色
    borderRadius: 8,            // 圆角
    elevation: 5,               // Android 阴影
    shadowColor: '#000',        // iOS 阴影颜色
    shadowOffset: {             // iOS 阴影偏移
      width: 0,
      height: 2,
    },
    shadowOpacity: 0.25,        // iOS 阴影透明度
    shadowRadius: 3.84,         // iOS 阴影半径
  }
});

2.Text 组件(文本显示):

javascript 复制代码
import { Text } from 'react-native';

const TextExample = () => {
  return (
    <Text
      numberOfLines={2}           // 最大行数
      ellipsizeMode="tail"        // 文本截断方式:'head', 'middle', 'tail', 'clip'
      selectable={true}           // 是否可选择
      style={styles.text}         // 样式
      onPress={() => {}}          // 点击事件
    >
      这是一段文本
    </Text>
  );
};

const styles = StyleSheet.create({
  text: {
    fontSize: 16,                 // 字体大小
    fontWeight: 'bold',           // 字体粗细
    color: '#333',               // 文字颜色
    textAlign: 'center',         // 文本对齐
    lineHeight: 24,              // 行高
    letterSpacing: 0.5,          // 字间距
    textDecorationLine: 'underline', // 文本装饰线
  }
});

3.Image 组件(图片显示)

javascript 复制代码
import { Image } from 'react-native';

const ImageExample = () => {
  return (
    <Image
      source={
  
  {                   // 图片源
        uri: 'https://example.com/image.jpg',
        // 或本地图片:source={require('./image.jpg')}
      }}
      style={styles.image}        // 样式
      resizeMode="cover"          // 缩放模式:'cover', 'contain', 'stretch', 'center'
      blurRadius={0}              // 模糊效果
      fadeDuration={300}          // 淡入动画时长
      onLoad={() => {}}           // 加载完成回调
      onError={() => {}}          // 加载错误回调
      defaultSource={require('./placeholder.jpg')} // 加载占位图
    />
  );
};

const styles = StyleSheet.create({
  image: {
    width: 200,                   // 宽度
    height: 200,                  // 高度
    borderRadius: 100,            // 圆角
  }
});

4.TouchableOpacity 组件(可点击容器):

javascript 复制代码
import { TouchableOpacity } from 'react-native';

const ButtonExample = () => {
  return (
    <TouchableOpacity
      style={styles.button}        // 样式
      activeOpacity={0.7}          // 按下时的透明度
      onPress={() => {}}           // 点击事件
      onLongPress={() => {}}       // 长按事件
      disabled={false}             // 是否禁用
    >
      <Text>点击我</Text>
    </TouchableOpacity>
  );
};

const styles = StyleSheet.create({
  button: {
    padding: 10,
    backgroundColor: '#007AFF',
    borderRadius: 5,
    alignItems: 'center',         // 子元素水平居中
    justifyContent: 'center',     // 子元素垂直居中
  }
});

5.TextInput 组件(输入框):

javascript 复制代码
import { TextInput } from 'react-native';

const InputExample = () => {
  const [text, setText] = useState('');

  return (
    <TextInput
      value={text}                 // 输入值
      onChangeText={setText}       // 值改变回调
      placeholder="请输入..."      // 占位文本
      placeholderTextColor="#999"  // 占位文本颜色
      keyboardType="default"       // 键盘类型
      secureTextEntry={false}      // 密码输入模式
      multiline={false}            // 多行输入
      maxLength={100}              // 最大长度
      autoCapitalize="none"        // 自动大写
      autoCorrect={false}          // 自动纠正
      style={styles.input}         // 样式
      onFocus={() => {}}          // 获得焦点回调
      onBlur={() => {}}           // 失去焦点回调
    />
  );
};

const styles = StyleSheet.create({
  input: {
    height: 40,
    borderWidth: 1,
    borderColor: '#ddd',
    borderRadius: 5,
    paddingHorizontal: 10,
    backgroundColor: '#fff',
  }
});

6.ScrollView 组件(滚动容器):

javascript 复制代码
import { ScrollView, RefreshControl } from 'react-native';

const ScrollExample = () => {
  const [refreshing, setRefreshing] = useState(false);

  return (
    <ScrollView
      style={styles.scrollView}    // 容器样式
      contentContainerStyle={styles.content} // 内容容器样式
      horizontal={false}           // 是否水平滚动
      showsVerticalScrollIndicator={false}   // 显示垂直滚动条
      bounces={true}              // iOS 弹性效果
      scrollEnabled={true}         // 是否可以滚动
      refreshControl={             // 下拉刷新控件
        <RefreshControl
          refreshing={refreshing}
          onRefresh={() => {
            setRefreshing(true);
            // 刷新逻辑
            setTimeout(() => setRefreshing(false), 2000);
          }}
        />
      }
    >
      {/* 滚动内容 */}
    </ScrollView>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    flex: 1,
  },
  content: {
    padding: 15,
  }
});

这些是最基础也是最常用的组件,每个组件都有很多配置项和样式属性。建议:

  • 根据实际需求选择合适的组件
  • 注意性能优化
  • 考虑平台差异(iOS/Android)
  • 合理使用样式和布局
相关推荐
道不尽世间的沧桑1 小时前
第17篇:网络请求与Axios集成
开发语言·前端·javascript
bin91534 小时前
DeepSeek 助力 Vue 开发:打造丝滑的复制到剪贴板(Copy to Clipboard)
前端·javascript·vue.js·ecmascript·deepseek
晴空万里藏片云6 小时前
elment Table多级表头固定列后,合计行错位显示问题解决
前端·javascript·vue.js
奶球不是球6 小时前
el-button按钮的loading状态设置
前端·javascript
无责任此方_修行中8 小时前
每周见闻分享:杂谈AI取代程序员
javascript·资讯
dorabighead9 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
林的快手11 小时前
CSS列表属性
前端·javascript·css·ajax·firefox·html5·safari
bug总结11 小时前
新学一个JavaScript 的 classList API
开发语言·javascript·ecmascript
网络安全-老纪11 小时前
网络安全-js安全知识点与XSS常用payloads
javascript·安全·web安全
yqcoder12 小时前
Express + MongoDB 实现在筛选时间段中用户名的模糊查询
java·前端·javascript