2014-02-27 1 views
3

나는이 원형 막대 내부에 백분율로 진행률을 씁니다. (예 : 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 방법 그려지는 텍스트의 원점의 좌표입니다. 나는 특히 RelativeLayout 또는 FrameLayout을 사용하는 것을 좋아했습니다. 필요한 경우 UX의 일관성을 위해 다양한 dpi에서 스타일을 사용하십시오.

6

이 작업을 시도해야합니다 : 당신은 또한 true로 centerInParent 세트, 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> 

.
그리고 비율을 표시하는 자세한 정보는 this tutorial 또는 this 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(); 
} 

}

관련 문제