在Android SDK中嵌套的形状嵌套、形状、Android、SDK

2023-09-07 09:46:37 作者:仅剩的年华丶用来掩饰忧伤

你好我想画有三个长方形的形状: 1.纯色2.等级3.白线,我该怎么办呢?当我尝试这一点,这是行不通的。该布局具有父颜色。

Hi I am trying to draw a shape that has three rectangular shapes: 1. solid color 2. gradient 3. white line How do I do that? When I try this, it does not work. The layout has the parent color.

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:height="60px"
    >
    <shape 
        android:shape="rectangle" 
        android:height="30px"
        >
        <solid 
            android:color="#ff297baf" 
            />
    </shape>
    <shape 
        android:shape="rectangle"
        android:height="30px"
        >
         <gradient
  android:type="linear"
  android:startColor="#ff297baf"
  android:endColor="#ff16c0e3"
  android:angle="270"/> 
    </shape>
    <shape
        android:shape="rectangle"
        android:height="3px"
        >
        <solid 
            android:color="#FFFFFFFF"
            />
    </shape>
</shape>

我试图做一个渐变的3种颜色。开始用纯色#ff297baf 60%开始从#ff297baf 渐变#ff16c0e3 并在末尾加上了一段时间线。

I am trying to make a gradient in 3 colors. Start with a solid color #ff297baf, at 60% start a gradient from #ff297baf to #ff16c0e3 and add a while line at the end.

推荐答案

如果你要考虑使用多个图形,你应该尝试LayerListDrawable。像这样的事情对我的作品,但只为你的例子60PX的高度。您可能需要修改,​​以满足您的需求完全,但是这应该给你一个良好的开端:

If you're going to look into using multiple shapes, you should try a LayerListDrawable. Something like this works for me, but only for your example height of 60px. You may have to modify it to suit your needs exactly, but this should get you a good start:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item>
        <shape
            android:shape="rectangle"
            >
            <gradient
                android:startColor="#ff297baf"
                android:endColor="#ff16c0e3"
                android:type="linear"
                android:angle="270"
                />
        </shape>
    </item>
    <item
        android:top="57px"
        >
        <shape
            android:shape="rectangle"
            >
            <solid
                android:color="#FFFFFFFF"
                />
            <size
                android:height="3px"
                />
        </shape>
    </item>
</layer-list>