问题
I would like to create ProgressBar
along with text same as below.
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:
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:
回答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