【问题标题】:How to display text in center of an indeterminate ProgressBar (spinning wheel)?如何在不确定的 ProgressBar(旋转轮)的中心显示文本?
【发布时间】:2014-09-22 15:51:15
【问题描述】:

如何在不确定的 ProgressBar(旋转轮)的中心显示文本?

我想在中间显示秒。使用安卓 API-19。 docs 表示要显示进度,必须使用水平条。 但是,如果我可以在中心显示文本,那么这对我有用。

再说一遍,如何设置文本以显示在旋转轮进度条中?

(我宁愿在跑之前尝试使用现有的 API 来构建自定义进度条)

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mycom.countdowntimer.CountdownMainActivity$PlaceholderFragment" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:indeterminate="true"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="87dp" />   
</RelativeLayout>

【问题讨论】:

  • 在 XML 中添加文本视图
  • @pedro 在哪里?见上面的xml
  • 进度条父级是什么布局?
  • @pedro 我的布局很灵活 - 可以将其更改为任何可行的内容。

标签: android


【解决方案1】:

将此文本添加到您的布局中

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="5 s"
    android:id="@+id/txt_secondsleft"
    android:layout_alignRight="@+id/progressBar1"
    android:layout_alignLeft="@+id/progressBar1"
    android:gravity="center"
    android:layout_alignBottom="@+id/progressBar1"
    android:layout_alignTop="@+id/progressBar1"
   />

你可以用它来显示你想要的文本,它会在你的进度条上居中

【讨论】:

  • 太棒了!我没有意识到 textview 可以通过设置布局对齐值显示在轮子的中心。
【解决方案2】:

我已经尝试并找到了解决方案。使用xml,它将对齐轮子中心的文本。希望这会对你有所帮助。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <ProgressBar
            android:id="@+id/prograss_bar"
            style="@android:style/Widget.ProgressBar.Large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="loading" />
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2014-08-12
    相关资源
    最近更新 更多