【问题标题】:Progressbar along with text in AndroidAndroid 中的进度条和文本
【发布时间】:2017-12-14 18:40:40
【问题描述】:

我想创建ProgressBar 以及与下面相同的文本。

请指导我。我开发了一些设计,但我无法在ProgressBar 的右侧显示文本,因为进度条占用最小宽度。请看下图:

我被困在这个问题上。提前致谢。

我的代码是:

 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_below="@+id/lnrButtons"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginRight="30dp"
            android:padding="5dp"
            android:background="@drawable/white_button_selector">

            <ProgressBar
                android:id="@+id/progressBarHome"
                android:layout_width="match_parent"
                style="?android:attr/progressBarStyleHorizontal"
                android:progressDrawable="@drawable/custom_progress_bar"
                android:layout_height="wrap_content"
                android:progress="50"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:indeterminate="false"/>

            <TextView
                android:id="@+id/txtProgress"
                android:layout_width="wrap_content"
                android:text="10%"
                android:textColor="@color/light_orange"
                android:background="@android:color/transparent"
                android:layout_centerVertical="true"
                android:layout_alignRight="@+id/progressBarHome"

                android:layout_height="wrap_content"/>

        </RelativeLayout>

实现代码更改后输出为:

【问题讨论】:

  • 尝试制作一个自定义新的,为完成的每项任务增加其宽度
  • @anddev:让我知道下面的答案对你有帮助吗?

标签: android progress-bar


【解决方案1】:

我想 Android ValueAnimator 应该可以为您解决问题。基本上,您需要为这个进度条设置一个自定义形状,当进度增加时,您可以设置动画以缩放其宽度。

您可能会在这里得到一些帮助 ObjectAnimator animate LinearLayout width

【讨论】:

  • 对你的文本视图使用 alignParentRight="true"
【解决方案2】:

你就按照那个来设计布局,我想你的布局设计会跟上来的。

例如。

<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
      style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       android:id="@+id/PROGRESS_BAR"
    >
    <TextView
        android:background="#00000000" // transparent background
        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>

根据您的要求,如果您想移动您的文本视图,您必须将您的进度值传递给以下txtView.setX((int) ur progress value);

希望对你有帮助。

【讨论】:

  • @anddev:你给进度条添加横向样式了吗?
  • 是的。请参阅我更新的问题。我已经添加了我的 xml 代码。
  • 按照您的代码实现后,请参考上图的输出。
  • 我认为它已经完成了,你只需通过 android:centerinparent="true" 将你的 textview 居中。它可以工作
  • 当我在第一张图片中显示时,我不需要在中心或任何固定位置显示文本,但随着进度的增加,文本视图必须随着进度一起移动。这是我的要求。
猜你喜欢
  • 1970-01-01
  • 2013-08-26
  • 2017-10-13
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 2020-09-17
  • 2023-04-02
相关资源
最近更新 更多