Progressbar along with text in Android

情到浓时终转凉″ 提交于 2021-01-28 12:06:34

问题


I would like to create ProgressBar along with text same as below.

enter image description here

Please guide me. I developed some design but I am not able to display text exactly right side of ProgressBar as progress bar take min width. Please see below image: enter image description here

I am stuck on this. Thanks in advance.

My Code is:

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

After implementing code changes output is:

enter image description here


回答1:


I suppose the Android ValueAnimator should do the trick for you. Basically you need a custom shape for this progress bar, which you can animate to scale its width as and when the progress increases.

You might get some help here ObjectAnimator animate LinearLayout width




回答2:


You just design the layout according to that.I think your layout design will be following.

For eg.

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

As per your requirement if you want to move your textview you have to pass your progress value to the following txtView.setX((int) ur progress value);

Hope it will helps you.



来源:https://stackoverflow.com/questions/29069984/progressbar-along-with-text-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!