内置文本的Progressbar android

内置文本的Progressbar android,android,android-progressbar,Android,Android Progressbar,我有一个循环的ProgressBar,我想在这个循环条中,用百分比(比如60%或70%)来写进度 如何在循环ProgressBar中执行此操作 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layou

我有一个循环的
ProgressBar
,我想在这个循环条中,用百分比(比如60%或70%)来写进度
如何在循环
ProgressBar
中执行此操作

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

        <ProgressBar
            android:id="@+id/progressBars"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"
            android:indeterminate="false"
            android:indeterminateDrawable="@drawable/progress_bar"
            android:max="100" />
    </RelativeLayout>

您可以在
ProgressBar
内部覆盖
onDraw
,并使用
Canvas.drawText
来决定文本的位置。您可以找到drwawText的文档:

x和y是正在绘制的文本原点的坐标


onDraw的方式在Android版本和设备之间提出了自己的一系列挑战。我特别喜欢使用
RelativeLayout
FrameLayout
。如果需要,可以跨各种dpi使用样式,以实现UX中的一致性

您应该尝试以下方法:

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

    <ProgressBar
        android:id="@+id/myProgress"
        android:layout_width="match_parent" android:layout_height="match_parent" />

    <TextView
        android:id="@+id/myTextProgress"
        android:layout_alignLeft="@id/myProgress" android:layout_alignTop="@id/myProgress"
        android:layout_alignRight="@id/myProgress" android:layout_alignBottom="@id/myProgress"
        android:background="@android:color/transparent" >

</RelativeLayout>  

您还可以将
centerInParent
设置为true,请参阅。
并查看或了解更多信息以显示您的百分比。

希望这会有所帮助。

检查进度条上的教程,如果进度条不正确,请查看@fllo的答案。@yams我重新编写了我的答案。OP没有在任何地方提到他想使用额外的
TextView
,所以我的回答没有错