自定义进度条 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11322740/
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
Custom Progress bar Android
提问by Seshu Vinay
I want to use this type of Progress bar in android. I have tried with many horizontal progress bars. They are all looking like default progress bars with different colors. Dont know how to use this type:
我想在 android 中使用这种类型的进度条。我尝试过许多水平进度条。它们看起来都像是具有不同颜色的默认进度条。不知道如何使用这种类型:
回答by Kumar Bibek
You will need to create your own custom progress bar. It's not as simple as using many horizontal bars.
您将需要创建自己的自定义进度条。它不像使用许多单杠那么简单。
回答by Seshu Vinay
Customizing a progress bar requires defining the attribute or properties for background and and progress of your progress bar.
自定义进度条需要为进度条的背景和进度定义一个或多个属性。
create a.xml file named customprogressbar.xml in your res-> drawable folder
在 res-> drawable 文件夹中创建一个名为 customprogressbar.xml 的 .xml 文件
customprogressbar.xml
自定义进度条.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define the background properties like color etc -->
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#000001"
android:centerColor="#0b131e"
android:centerY="1.0"
android:endColor="#0d1522"
android:angle="270"
/>
</shape>
</item>
<!-- Define the progress properties like start color, end color etc -->
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#007A00"
android:centerColor="#007A00"
android:centerY="1.0"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
Now you need to set the to set the progressDrawable property to customprogressbar.xml(drawable)
现在您需要设置将 progressDrawable 属性设置为 customprogressbar.xml(drawable)
you can do it in xml file or in Activity(at run time)
您可以在 xml 文件或活动中(在运行时)
In your xml do like following
在您的 xml 中执行以下操作
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/custom_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
at runtime do the following
在运行时执行以下操作
// Get the Drawable custom_progressbar
Drawable draw= res.getDrawable(R.drawable.custom_progressbar);
// set the drawable as progress drawavle
progressBar.setProgressDrawable(draw);