我尝试了react-native-hr包,但在Android和iOS上都无法正常工作。
以下代码也不适合,因为它会在末尾渲染三个点
<Text numberOfLines={1}}>
______________________________________________________________
</Text>
我尝试了react-native-hr包,但在Android和iOS上都无法正常工作。
以下代码也不适合,因为它会在末尾渲染三个点
<Text numberOfLines={1}}>
______________________________________________________________
</Text>
您可以使用一个带有底部边框的空视图。
<View
style={{
borderBottomColor: 'black',
borderBottomWidth: StyleSheet.hairlineWidth,
}}
/>
borderBottomWidth: StyleSheet.hairlineWidth
:) - Ryan Pergent我能使用flexbox
属性绘制一条分隔线,即使文本位于行的中心。
<View style={{flexDirection: 'row', alignItems: 'center'}}>
<View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
<View>
<Text style={{width: 50, textAlign: 'center'}}>Hello</Text>
</View>
<View style={{flex: 1, height: 1, backgroundColor: 'black'}} />
</View>
<Text style={{textAlign: 'center', paddingHorizontal:8}}>你好朋友们!</Text>
- kopacabana可以使用margin来改变一条线的宽度并将其置于中心位置。
import { StyleSheet } from 'react-native;
<View style = {styles.lineStyle} />
const styles = StyleSheet.create({
lineStyle:{
borderWidth: 0.5,
borderColor:'black',
margin:10,
}
});
如果您想动态地设置边距,那么您可以使用尺寸宽度(Dimension width)。
<View style = {styles.lineStyle} />
,因为中间没有任何内容;-) - Martin Nordströmstyles.lineStyle
应该对应于这里的const styles = StyleSheet.create({ lineStyle: ... });
。而你只有lineStyle = { ...}
,这会导致问题。一个完整的解决方案是使用styles.lineStyle
,可以这样写:const styles = StyleSheet.create({ lineStyle: { borderWidth: 0.5, borderColor: 'black', margin: 10 } });
。 - kevrlineStyle
放在了你的 StyleSheet 中。 - Smit Thakkar我是这样做的。希望能对你有帮助。
<View style={styles.hairline} />
<Text style={styles.loginButtonBelowText1}>OR</Text>
<View style={styles.hairline} />
样式:
hairline: {
backgroundColor: '#A2A2A2',
height: 2,
width: 165
},
loginButtonBelowText1: {
fontFamily: 'AvenirNext-Bold',
fontSize: 14,
paddingHorizontal: 5,
alignSelf: 'center',
color: '#A2A2A2'
},
react-native-hr-component
。npm i react-native-hr-component --save
您的代码:
import Hr from 'react-native-hr-component'
//..
<Hr text="Some Text" fontSize={5} lineColor="#eee" textPadding={5} textStyles={yourCustomStyles} hrStyles={yourCustomHRStyles} />
创建可重用的Line
组件对我很有效:
import React from 'react';
import { View } from 'react-native';
export default function Line() {
return (
<View style={{
height: 1,
backgroundColor: 'rgba(255, 255, 255 ,0.3)',
alignSelf: 'stretch'
}} />
)
}
现在,可以在任何地方导入并使用 Line
:
<Line />
以下是今天更新的 React Native(JSX)代码:
<View style = {styles.viewStyleForLine}></View>
const styles = StyleSheet.create({
viewStyleForLine: {
borderBottomColor: "black",
borderBottomWidth: StyleSheet.hairlineWidth,
alignSelf:'stretch',
width: "100%"
}
})
您可以使用alignSelf:'stretch'
或width: "100%"
两种方式都可以...
borderBottomWidth: StyleSheet.hairlineWidth
这里 StyleSheet.hairlineWidth
是最细的,
然后,
borderBottomWidth: 1,
等等,以增加线条的厚度。
import { View, Dimensions } from 'react-native'
var { width, height } = Dimensions.get('window')
// Create Component
<View style={{
borderBottomColor: 'black',
borderBottomWidth: 0.5,
width: width - 20,}}>
</View>
npm install --save react-native-elements
# or with yarn
yarn add react-native-elements
import { Divider } from 'react-native-elements'
<Divider style={{ backgroundColor: 'blue' }} />
<Divider />
组件,这可能有些过度,但是无论如何,您都应该使用库来节省时间并样式化简单的按钮、搜索栏等等...查看它为您的应用程序所能做的一切
https://react-native-elements.github.io/react-native-elements/ - Jonathan Sanchez