可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a circular ProgressBar
, I want inside this circular bar, write the progression in percentage (like 60 %, or 70 %..)
How can I do this inside the circular ProgressBar
?
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBars" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:indeterminate="false" android:indeterminateDrawable="@drawable/progress_bar" android:max="100" /> </RelativeLayout>
回答1:
I do not think is directly achievable. You should override onDraw
inside ProgressBar
, and use Canvas.drawText
to decide where the text should be positioned . Here you can find the documentation for drwawText:
x and y are the coordinates of the origin of the text being drawn
回答2:
You should try this:
<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:id="@+id/myProgress" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/myTextProgress" android:layout_alignLeft="@id/myProgress" android:layout_alignTop="@id/myProgress" android:layout_alignRight="@id/myProgress" android:layout_alignBottom="@id/myProgress" android:background="@android:color/transparent" > </RelativeLayout>
You also can with the centerInParent
set to true, see this.
And see this tutorial or this one for more info to display your percentage.
Hope this will help.
回答3:
Check this link it is the best way as my point of view
Make TextProgressBar
public class TextProgressBar extends ProgressBar { private String text; private Paint textPaint; public TextProgressBar(Context context) { super(context); text = "HP"; textPaint = new Paint(); textPaint.setColor(Color.BLACK); } public TextProgressBar(Context context, AttributeSet attrs) { super(context, attrs); text = "HP"; textPaint = new Paint(); textPaint.setColor(Color.BLACK); } public TextProgressBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); text = "HP"; textPaint = new Paint(); textPaint.setColor(Color.BLACK); } @Override protected synchronized void onDraw(Canvas canvas) { // First draw the regular progress bar, then custom draw our text super.onDraw(canvas); Rect bounds = new Rect(); textPaint.getTextBounds(text, 0, text.length(), bounds); int x = getWidth() / 2 - bounds.centerX(); int y = getHeight() / 2 - bounds.centerY(); canvas.drawText(text, x, y, textPaint); } public synchronized void setText(String text) { this.text = text; drawableStateChanged(); } public void setTextColor(int color) { textPaint.setColor(color); drawableStateChanged(); }
}
回答4:
The onDraw
way presents its own set of challenges between Android versions and devices. I have particularly liked using a RelativeLayout
or FrameLayout
. If need be, use styles across various dpi's for consistency in UX.