Android - 在进度条的中心显示文本

ahm*_*mad 30 android progress-bar

我已经制作了一个水平进度条,它完美无缺.我想在栏中间显示一个textview或类似的东西,显示倒计时栏正在加载.请记住,它不是进度对话框,进度条位于活动内部并显示倒数计时器.任何人都可以帮助我,最好的方法是什么,我怎么能做到这一点?

Mat*_*ris 41

如果你ProgressBar和你TextView在里面RelativeLayout你可以给ProgressBar一个id,然后TextViewProgressBar使用它对齐.它应该显示在最重要的ProgressBar.确保背景是透明的,这样你仍然可以看到ProgressBar

例如:

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        // other attributes
        android:id="@+id/PROGRESS_BAR"
    >
    <TextView
        android:background="#00000000" // transparent
        // other attributes
        android:layout_alignLeft="@id/PROGRESS_BAR"
        android:layout_alignTop="@id/PROGRESS_BAR"
        android:layout_alignRight="@id/PROGRESS_BAR"
        android:layout_alignBottom="@id/PROGRESS_BAR"
    >
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

  • 需要 textView 和 ProgressBar 元素上的结束标记 (3认同)

Yan*_*ann 12

您可以使用FrameLayout在ProgressBar上显示TextView:

...
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progress"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:progressDrawable="@drawable/progressbar" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />
        ...
    </RelativeLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)


You*_*yed 5

您可以在其中设置 带有RelativeLayout 的LinearLayout并使 RelativeLayout高度宽度等于wrap_content然后在 ProgressBar 下的 TextView 您将添加 android: layout_centerInParent="true"

例子 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:max="100"
            android:progress="65" />
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="@color/white"
            android:text="6:00 AM"
            android:textSize="25sp"
            android:textStyle="bold"/>
    </RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)