【问题标题】:How to create a custom ProgressBar with drawables, without programming?如何在不编程的情况下创建带有可绘制对象的自定义 ProgressBar?
【发布时间】:2019-02-16 12:19:48
【问题描述】:

这是我自定义的白色透明滚轮 ProgressBar

progressbar_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="2.5"
    android:thickness="4dp"
    android:useLevel="false">
    <gradient
        android:type="sweep"
        android:startColor="@android:color/white"
        android:centerColor="@android:color/white"
        android:endColor="@android:color/transparent"
    />
</shape>


在 Android Studio 的预览窗口中,该元素已正确显示。
但在模拟器 (Android 9) 和设备 (Android 6) 中进行调试测试后,ProgressBar 显示为@color/colorAccent wheel Progressbar。

我尝试过更改 android:shape、android:innerRadiusRatio、android:thickness,甚至将渐变更改为纯色。没有任何改变。


这是我的 progress_task_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<ImageView
    android:id="@+id/correct_icon"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/correct_icon"
    android:padding="@dimen/padding_large"
    android:visibility="gone" />

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:padding="@dimen/padding_large"
    android:progressDrawable="@drawable/progressbar_circle"
    android:visibility="gone" />

</RelativeLayout>


如何在 xml 上实现自定义 android ProgressBar 而无需在 java 中动态编程?

【问题讨论】:

  • 如果你的 min api > = 21 你可以使用android:indeterminateTint="@android:color/white"
  • @NongthonbamTonthoi 我设置 min api = 14 android:indeterminateTint="@android:color/white" 用于纯色,渐变色怎么样。 (我稍后会处理旧的 api 版本。)

标签: android android-xml android-progressbar


【解决方案1】:

在drawable文件夹中创建progress.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360">

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false">

        <size
            android:width="76dip"
            android:height="76dip" />

        <gradient
            android:angle="0"
            android:endColor="#1672B6"
            android:startColor="@android:color/transparent"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>

然后像这样使用它:

<ProgressBar
                android:id="@+id/pbarLoading"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:indeterminateDrawable="@drawable/progress"/>

【讨论】:

  • android:indeterminateDrawable="@drawable/progress" 是这种情况的神奇解决方案。我可以将 "@drawable/progress" 更改为我的 "@drawable/progressbar_circle"。此外, xml 标签使我的 "@drawable/progressbar_circle" 动画化。所有问题都解决了。非常感谢你。但是,我的声誉不到 15,因为这是我在这里的第一个问题。我还不能投票。 :)
猜你喜欢
  • 2021-11-06
  • 1970-01-01
  • 2021-08-11
  • 2016-07-22
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2022-11-20
相关资源
最近更新 更多