2013-08-23 3 views
24

の中央にテキストを表示します。バーがロードされているときにカウントダウンを表示しているテキストビューなどを真ん中に表示したいと思います。進捗ダイアログではなく、アクティビティ内にプログレスバーがあり、カウントダウンタイマーが表示されます。誰もが私を助けることができる、これを行うための最善の方法とはどのように私はこれを達成することができますか?アンドロイド - 私はすでに水平プログレスバーを作ったし、それが完璧に動作プログレスバー

答えて

33

あなたProgressBarTextViewあなたはProgressBarにIDを与え、その後、これを使用してProgressBarTextViewを揃えることができRelativeLayout内にある場合。それはProgressBarの上に表示されます。

<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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     // other attributes 
     android:id="@+id/PROGRESS_BAR" 
    > 
    <TextView 
     android:background="#00000000" // transparent 
     // other attributes 
     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> 
+0

のみ、このアンドロイド:layout_alignTop = "@のID/simpleProgressBar" 作業罰金私のために。 –

8

あなたはプログレスバー上のTextViewを表示するためにでframeLayoutを使用することができます:あなたはまだたとえばProgressBar

を見ることができるように、背景が透明であることを確認し

... 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ProgressBar 
     android:id="@+id/progress" 
     style="@android:style/Widget.ProgressBar.Horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:progressDrawable="@drawable/progressbar" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" /> 
     ... 
    </RelativeLayout> 
</FrameLayout> 
+0

良い解決策。相対レイアウトの目的が不明な場合 - 余計なようです。 – dazed

+0

は、重なっ子供のいないさまざまな画面サイズにスケーラブルだ方法で、子ビューを整理することが困難な場合があるためでframeLayoutは、単一の子ビューを保持するために使用されなければならない@dazed。あなたは、しかし、でframeLayoutに複数の子を追加し、アンドロイド使用して、それぞれの子に重力を割り当てることによってでframeLayout内の自分の位置を制御することができます。layout_gravity属性を。 出典:あなたは多くの意見を入れて、相対的にそれらを整理したいときに履歴書にhttps://developer.android.com/reference/android/widget/FrameLayout.html 、RelativeLayoutが良いです。 – androidevil

4

最も簡単な方法!

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

を行います

   <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
        <ProgressBar 
         android:id="@+id/progressBar" 
         style="@android:style/Widget.ProgressBar.Horizontal" 
         android:progressDrawable="@drawable/customprogressbar" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:max="100"/> 

        <TextView 
         android:id="@+id/progressBarinsideText" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_alignParentRight="true" 
         android:layout_centerVertical="true" 
         android:gravity="center" 
        /> 
       </RelativeLayout> 
0

これは、私は、Webビューを中心としたスピナー上記プログレステキストを表示するために使用されるものです。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <WebView 
     android:id="@+id/help_webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#F22" 
     android:scrollbars="none" /> 


    <ProgressBar 
     android:id="@+id/progressBar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:layout_centerVertical="true" /> 

    <TextView 
     android:id="@+id/progressBarMessage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Loading, please wait..." 
     android:layout_centerInParent="true" 
     android:layout_above="@id/progressBar" 
     android:background="#00000000" /> 


</RelativeLayout> 
関連する問題