【问题标题】:Generate gradient progress bar生成渐变进度条
【发布时间】:2016-07-22 11:49:24
【问题描述】:

我正在使用以下代码在进度条中显示渐变。那么如何创建如上图所示的渐变进度条呢?我尝试了很多解决方案,但都没有成功。

  <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:startColor="@color/gray"
                android:centerColor="@color/gray"
                android:centerY="0.75"
                android:endColor="@color/gray"
                android:angle="270"
                />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>
    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="5dip" />
                <gradient
                    android:startColor="@color/dark_yellow"
                    android:endColor="@color/dark_yellow"
                    android:angle="270" />
            </shape>
        </clip>
    </item>

</layer-list>

【问题讨论】:

标签: android


【解决方案1】:

在您的情况下,@android:id/secondaryProgress 不是必需的。此外,android:angle=270 会从上到下旋转渐变,因此它与期望的方向垂直。

你只需要:

drawable/gradient_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dp"/>
            <solid android:color="#fff"/>
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <shape>
            <corners android:radius="5dp"/>
            <gradient
                android:endColor="@android:color/holo_red_dark"
                android:startColor="@android:color/holo_orange_light"
                />
        </shape>
    </item>

</layer-list>

布局/any_layout_file.xml

<ProgressBar
    xmlns:tools="http://schemas.android.com/tools"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:progressDrawable="@drawable/gradient_progress"
    tools:progress="60"
    />

【讨论】:

  • 这几乎可以工作了,只需要使用 标签将带有 id/progress 的项目包装起来
猜你喜欢
  • 1970-01-01
  • 2022-11-13
  • 2017-02-09
  • 2018-11-05
  • 1970-01-01
  • 2018-09-18
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多