React Native 不确定高度容器的虚线边框绘制方法
可以使用绝对定位的方法,在容器中添加一个覆盖整个容器的透明视图,然后在透明视图中使用js画虚线边框。
具体步骤如下:
-
在容器中添加一个覆盖整个容器的透明视图,并设置该视图的position为absolute。
-
在透明视图中使用绝对定位的方式画虚线边框。可以使用react-native-svg库中的Line组件来实现。
-
根据需要设置虚线的样式,如线条颜色、宽度、间隔等。
示例代码如下:
import React from 'react';
import { View } from 'react-native';
import Svg, { Line } from 'react-native-svg';
export default class DashedBorder extends React.Component {
render() {
const { containerStyle, dashStyle } = this.props;
return (
<View style={containerStyle}>
<View style={{ position: 'absolute', top: 0, bottom: 0, left: 0, right: 0 }}>
<Svg height='100%' width='100%'>
<Line
x1='0'
y1='0'
x2='100%'
y2='0'
stroke={dashStyle.color}
strokeWidth={dashStyle.width}
strokeDasharray={`${dashStyle.dashLength},${dashStyle.spaceLength}`}
/>
<Line
x1='100%'
y1='0'
x2='100%'
y2='100%'
stroke={dashStyle.color}
strokeWidth={dashStyle.width}
strokeDasharray={`${dashStyle.dashLength},${dashStyle.spaceLength}`}
/>
<Line
x1='100%'
y1='100%'
x2='0'
y2='100%'
stroke={dashStyle.color}
strokeWidth={dashStyle.width}
strokeDasharray={`${dashStyle.dashLength},${dashStyle.spaceLength}`}
/>
<Line
x1='0'
y1='100%'
x2='0'
y2='0'
stroke={dashStyle.color}
strokeWidth={dashStyle.width}
strokeDasharray={`${dashStyle.dashLength},${dashStyle.spaceLength}`}
/>
</Svg>
</View>
{this.props.children}
</View>
);
}
}
使用方式:
<DashedBorder
containerStyle={{ flex: 1 }}
dashStyle={{ color: 'red', width: 2, dashLength: 5, spaceLength: 5 }}
>
// 容器中的内容
</DashedBorder>
原文地址: https://www.cveoy.top/t/topic/oYV2 著作权归作者所有。请勿转载和采集!