import { View, StyleSheet } from 'react-native';
const MyComponent = () => {
return (
{/* 子组件 */}
);
};
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 阴影半径
}
});
import { Text } from 'react-native';
const TextExample = () => {
return (
{}} // 点击事件
>
这是一段文本
);
};
const styles = StyleSheet.create({
text: {
fontSize: 16, // 字体大小
fontWeight: 'bold', // 字体粗细
color: '#333', // 文字颜色
textAlign: 'center', // 文本对齐
lineHeight: 24, // 行高
letterSpacing: 0.5, // 字间距
textDecorationLine: 'underline', // 文本装饰线
}
});
import { Image } from 'react-native';
const ImageExample = () => {
return (
{}} // 加载完成回调
onError={() => {}} // 加载错误回调
defaultSource={require('./placeholder.jpg')} // 加载占位图
/>
);
};
const styles = StyleSheet.create({
image: {
width: 200, // 宽度
height: 200, // 高度
borderRadius: 100, // 圆角
}
});
import { TouchableOpacity } from 'react-native';
const ButtonExample = () => {
return (
{}} // 点击事件
onLongPress={() => {}} // 长按事件
disabled={false} // 是否禁用
>
点击我
);
};
const styles = StyleSheet.create({
button: {
padding: 10,
backgroundColor: '#007AFF',
borderRadius: 5,
alignItems: 'center', // 子元素水平居中
justifyContent: 'center', // 子元素垂直居中
}
});
import { TextInput } from 'react-native';
const InputExample = () => {
const [text, setText] = useState('');
return (
{}} // 获得焦点回调
onBlur={() => {}} // 失去焦点回调
/>
);
};
const styles = StyleSheet.create({
input: {
height: 40,
borderWidth: 1,
borderColor: '#ddd',
borderRadius: 5,
paddingHorizontal: 10,
backgroundColor: '#fff',
}
});
import { ScrollView, RefreshControl } from 'react-native';
const ScrollExample = () => {
const [refreshing, setRefreshing] = useState(false);
return (
{
setRefreshing(true);
// 刷新逻辑
setTimeout(() => setRefreshing(false), 2000);
}}
/>
}
>
{/* 滚动内容 */}
);
};
const styles = StyleSheet.create({
scrollView: {
flex: 1,
},
content: {
padding: 15,
}
});
这些是最基础也是最常用的组件,每个组件都有很多配置项和样式属性。建议: