很简单的方法实现带数字的水平方向进度条

我们先来看一下要实现效果图
效果图

为了实现类似的效果,一般是通过继承ProgressBar或者自定义View的方法来实现。
觉得这种方法有点繁琐,应该有更简单的方法,于是查阅ProgressBar相关属性,有这样一句话:“To change to a horizontal progress bar, apply the Widget.ProgressBar.Horizontal style”,发现设置style为“style=”@android:style/Widget.ProgressBar.Horizontal””,就可以设置水平显示。API截图如下:
API

于是使用了一个简单的方法,来实现这样的效果。布局相关代码如下:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp" >

        <ProgressBar 
            android:id="@+id/my_progress"
            android:layout_width="match_parent"
            android:layout_height="24dp"
            android:max="100"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:progressDrawable="@drawable/progressbar_color"
            android:textAlignment="center"/>

        <TextView 
            android:id="@+id/tvContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:layout_centerInParent="true"  />
    </RelativeLayout>

调用的地方,直接使用setProgress()和setText()来设置进度和百分比数字就OK了。

原文地址:https://www.cnblogs.com/lishbo/p/9956046.html