Android 如何创建自定义水平进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20306710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:37:21  来源:igfitidea点击:

How to create Custom Horizontal progress bar

androidprogress-bar

提问by Amon Olimov

Please help to create horizontal progress bar like thisenter image description here

请帮助创建这样的水平进度条在此处输入图片说明

采纳答案by plattnum

You do not need a custom progress bar. Use style="@android:style/Widget.ProgressBar.Horizontal"in your layout xml file. You will also need to use ProgressBar.incrementProgressBy(int progress);

您不需要自定义进度条。使用style="@android:style/Widget.ProgressBar.Horizontal"你的布局XML文件中。您还需要使用 ProgressBar.incrementProgressBy(int progress);

See Android Developers|ProgressBar

查看Android 开发者|ProgressBar

回答by Ranjith Kumar

Set style for horizontal progress bar

设置水平进度条的样式

        style="?android:attr/progressBarStyleHorizontal"

Create custom progress drawable: green_progress_drawable.xml

创建自定义进度可绘制:green_progress_drawable.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="5dip" />
        <gradient
            android:startColor="#779d9e9d"
            android:centerColor="#995a5d5a"
            android:centerY="0.75"
            android:endColor="#ff747674"
            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="#33FF33"
                android:endColor="#008000"
                android:angle="270" />
        </shape>
    </clip>
</item>
</layer-list>

My progressbar code:

我的进度条代码:

 <ProgressBar
        android:progressDrawable="@drawable/green_progress_drawable"
        style="?android:attr/progressBarStyleHorizontal"

        android:id="@+id/mf_progress_bar"
        app:layout_widthPercent="80%"
        app:layout_heightPercent="8%"
        app:layout_marginTopPercent="5%"
        android:layout_gravity="center_horizontal"
        />

Code credit to @Ryan https://stackoverflow.com/a/5745923/3879847

代码归功于@Ryan https://stackoverflow.com/a/5745923/3879847

My output:

我的输出:

enter image description hereI hope this post maybe helpful for someone..

在此处输入图片说明我希望这篇文章可能对某人有帮助..