【问题标题】:Progressbar android with text insideProgressbar android里面有文字
【发布时间】:2014-02-27 09:42:01
【问题描述】:

我有一个圆形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>

【问题讨论】:

标签: android android-progressbar


【解决方案1】:

你应该试试这个:

<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,请参阅this
并查看this tutorialthis one 了解更多信息以显示您的百分比。
希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您可以在ProgressBar 中覆盖onDraw 来实现它,并使用Canvas.drawText 来决定文本的位置。 Here你可以找到drwawText的文档:

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

    【讨论】:

    • 这不是真的,看看@fllo的回答。
    • @yams 我改写了我的答案。 OP 没有提到他想使用额外的TextView,所以我的回答没有错误
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      相关资源
      最近更新 更多