如何在Android中将圆形进度条设置在按钮内

6

我想在按钮内制作圆形进度条,但我不想使用像dmytrodanylyk/circular-progress-button这样的库。

我认为可以使用drawable来解决。

所以,我制作了circular_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:duration="1"
        android:toDegrees="360" >

        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="8"
            android:useLevel="false" >
            <size
                android:height="48dip"
                android:width="48dip" />

            <gradient
                android:centerColor="#f0f0f0"
                android:centerY="0.50"
                android:endColor="#000000"
                android:startColor="#ffffff"
                android:type="sweep"
                android:useLevel="false" />
        </shape>

    </rotate>

并且,
<Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Log In"
                android:layout_below="@+id/passwordWrapper"
                android:layout_marginTop="5dp"
                android:theme="@style/AppTheme.SignButton"
                android:id="@+id/loginButton"
                android:drawableRight="@drawable/circular_progress"
                />

在这里输入图片描述

但是,进度条已停止。

第二个解决方案是创建一个带有进度条的自定义按钮类。

但我不知道如何创建带有进度条的自定义按钮类。

1个回答

6

像这样在您的按钮中使用 ProgressBar

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Log In"
    android:layout_below="@+id/passwordWrapper"
    android:layout_marginTop="5dp"
    android:theme="@style/AppTheme.SignButton"
    android:id="@+id/loginButton"
    />
<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>
</RelativeLayout>

在您的活动中初始化进度条,可以像这样进行:
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);

当你想让进度条显示出来时,请添加以下代码:

    progressBar.setVisibility(View.VISIBLE);

当您想要隐藏进度条时,请添加以下内容:

    progressBar.setVisibility(View.GONE);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接