gpt4 book ai didi

android - 在android中创建矩形进度条

转载 作者:搜寻专家 更新时间:2023-11-01 08:54:27 24 4
gpt4 key购买 nike

enter image description here

你好,我想创建一个进度条,它看起来应该像上图一样。在这里,我创建了一个如下图所示的图像。

enter image description here

下面是我在res下的drawable文件夹中创建的xml文件,

进度.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 android:shape="rectangle">
<solid android:color="@color/white"/>
</shape>
</item>

<item android:id="@android:id/progress">
<clip>
<shape android:shape="rectangle">
<solid android:color="@color/lightgreen"/>
</shape>
</clip>
</item>

</layer-list>

但是我想要在progress继续的时候和图1一模一样的进度条,怎么实现呢?

最佳答案

自定义进度条需要为进度条的背景和进度定义一个或多个属性。

res->drawable 文件夹中创建一个名为 customprogressbar.xml 的 xml 文件

customprogressbar.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>

现在您需要将 progressDrawable 属性设置为 customprogressbar.xml(drawable)

您可以在 xml 文件或 Activity(在运行时)中完成

在你的 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" />

在运行时执行以下操作

  // Get the Drawable custom_progressbar                     
Drawable draw= res.getDrawable(R.drawable.custom_progressbar);
// set the drawable as progress drawavle
progressBar.setProgressDrawable(draw);

有关更多详细信息,请查看 HERE

同时检查 How to Customize ProgressBar in Android?

关于android - 在android中创建矩形进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com