2014-02-27 20 views
3

的android文本我有一个圆形ProgressBar,我想这个圆形酒吧内,以百分比写进程(如60%或70%..)
我怎样才能做到这一点的圆形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

我不认为是可以直接实现的。您应该覆盖ProgressBar中的onDraw,并使用Canvas.drawText来决定文本的位置。 Here你可以找到文档drwawText:

x和y正在制定

0

onDraw方式提出了自己的一套的Android版本和设备之间的挑战文字的原点的坐标。我特别喜欢使用RelativeLayoutFrameLayout。如果需要,请使用各种dpi的样式来保持UX的一致性。

6

你应该试试这个:

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

您也可以与centerInParent设置为true,看this
并参阅this tutorialthis one了解更多信息以显示您的百分比。
希望这会有所帮助。

3

Check this link it is the best way as my point of view

让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(); 
} 

}