进度条android,里面有文字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22064572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:29:27  来源:igfitidea点击:

Progressbar android with text inside

androidandroid-progressbar

提问by user3278732

I have a circular ProgressBar, I want inside this circular bar, write the progression in percentage (like 60 %, or 70 %..)
How can I do this inside the circular 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: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>

采纳答案by Blackbelt

You could achieve it overriding onDrawinside ProgressBar, and use Canvas.drawTextto decide where the text should be positioned . Hereyou can find the documentation for drwawText:

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

x and y are the coordinates of the origin of the text being drawn

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

回答by fllo

You should try this:

你应该试试这个:

<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>  

You also can with the centerInParentset to true, see this.
And see this tutorialor this onefor more info to display your percentage.
Hope this will help.

您也可以将centerInParent设置为 true,请参阅
看看本教程这一个更多的信息,以显示您的百分比。
希望这会有所帮助。

回答by Siddharth

The onDrawway presents its own set of challenges between Android versions and devices. I have particularly liked using a RelativeLayoutor FrameLayout. If need be, use styles across various dpi's for consistency in UX.

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