如何在react-native-svg元素中实现fill属性

我有一个用路径/线绘制的简单三角形,从AdobeXD作为SVG导出,然后将SVG代码复制并调整到具有react-native-svg库元素的react-native项目中。我试图填充元素的背景,因为我不希望它是透明的。我认为这就像在G元素中使用Web SVG的fill属性一样简单,但是它不起作用。

这里是SVG代码:

<Svg width="22.364" height="20.315" viewBox="0 0 22.364 20.315">
    <G fill="#fff" transform="translate(0.682 0.632)">
        <Line
            id="Line_37"
            data-name="Line 37"
            x1="21"
            y1="12"
            transform="translate(0 7)"
            stroke="rgba(69,74,102,.5)"
            stroke-linecap="round"
            stroke-width="1"
        />
        <Line
            id="Line_38"
            data-name="Line 38"
            y2="19"
            transform="translate(21)"
            stroke="rgba(69,.5)"
            stroke-linecap="round"
            stroke-width="1"
        />
        <Line
            id="Line_40"
            data-name="Line 40"
            x1="21"
            y2="7"
            stroke="#fff"
            stroke-linecap="round"
            stroke-width="1"
        />
    </G>
</Svg>

看过react-native-svg的文档,他们说“填充道具指的是形状内的颜色。”但是就像我说的那样,fill道具在<Svg>元素和<G>元素中都无法工作...有什么想法吗?

wangjiajunwf 回答:如何在react-native-svg元素中实现fill属性

fill上设置G属性将影响子元素(在您的情况下为Line元素),而不是整个组。

您可以绘制一个覆盖整个SVG的白色矩形以完成所需的操作:

<Svg width="22.364" height="20.315" viewBox="0 0 22.364 20.315">
<Rect
    x="0"
    y="0"
    width="100%"
    height="100%"
    fill="white"
/>
<G fill="#fff" transform="translate(0.682 0.632)">
    <Line
        id="Line_37"
        data-name="Line 37"
        x1="21"
        y1="12"
        transform="translate(0 7)"
        stroke="rgba(69,74,102,.5)"
        stroke-linecap="round"
        stroke-width="1"
    />
    <Line
        id="Line_38"
        data-name="Line 38"
        y2="19"
        transform="translate(21)"
        stroke="rgba(69,.5)"
        stroke-linecap="round"
        stroke-width="1"
    />
    <Line
        id="Line_40"
        data-name="Line 40"
        x1="21"
        y2="7"
        stroke="#fff"
        stroke-linecap="round"
        stroke-width="1"
    />
</G>

本文链接:https://www.f2er.com/2815046.html

大家都在问