2013-08-23 33 views
24

Ho già eseguito una barra di avanzamento orizzontale e funziona perfettamente. Mi piacerebbe mostrare una vista testuale o qualcosa di simile proprio nel mezzo di esso mostrando un conto alla rovescia mentre la barra si sta caricando. Tieni presente che non è una finestra di avanzamento, la barra di avanzamento si trova all'interno di un'attività e mostra un timer per il conto alla rovescia. Qualcuno può darmi una mano, qual è il modo migliore per farlo e come posso farlo?Android - visualizzazione del testo al centro della barra di avanzamento

risposta

33

Se il ProgressBar e TextView sono all'interno di una RelativeLayout si può dare la ProgressBar un id, e quindi allineare la TextView con l'ProgressBar utilizzando tale. Dovrebbe quindi mostrare in cima allo ProgressBar. Assicurarsi che lo sfondo è trasparente in modo che si può ancora vedere la ProgressBar

Ad esempio:

<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

Solo questo android: layout_alignTop = "@ id/simpleProgressBar" funziona bene per me. –

8

È possibile utilizzare un FrameLayout per visualizzare il TextView sul 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

Buona soluzione. Non sono sicuro dello scopo del layout relativo - sembra superfluo. – dazed

+0

@dazed FrameLayout deve essere utilizzato per contenere una visualizzazione figlio singola, perché può essere difficile organizzare le viste secondarie in un modo che sia scalabile a diverse dimensioni dello schermo senza che i bambini si sovrappongano l'un l'altro. Tuttavia, puoi aggiungere più bambini a FrameLayout e controllare la loro posizione all'interno di FrameLayout assegnando la gravità a ciascun bambino, utilizzando l'attributo android: layout_gravity. Fonte: https://developer.android.com/reference/android/widget/FrameLayout.html In ripresa, RelativeLayout è migliore quando si desidera mettere molte viste e organizzarle relativamente l'una all'altra. – androidevil

4

modo più semplice !

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

Fai 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

che è ciò che ha funzionato per me:

   <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

Questo è quello che ho usato per visualizzare il testo progresso sopra un filatore incentrata su una vista 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> 
Problemi correlati