如何使用drawable创建自定义ProgressBar,无需编程?

问题描述 投票:2回答:1

这是我自定义的白色透明轮子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中动态编程?

android android-xml android-progressbar
1个回答
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"/>
© www.soinside.com 2019 - 2024. All rights reserved.