2013-08-23 10 views
24

Wykonałem już poziomy pasek postępu i działa idealnie. Chciałbym wyświetlić widok tekstowy lub coś podobnego od samego początku, pokazując odliczanie podczas ładowania paska. Należy pamiętać, że nie jest to okno dialogowe postępu, pasek postępu znajduje się wewnątrz działania i pokazuje zegar odliczający. Czy ktoś może mi pomóc, jaki jest najlepszy sposób na zrobienie tego i jak mogę to zrobić?Android - wyświetlanie tekstu na środku paska postępu

Odpowiedz

33

Jeżeli Państwa ProgressBar i TextView są wewnątrz RelativeLayout można dać ProgressBar identyfikator, a następnie wyrównać TextView z ProgressBar używając tego. Powinien wtedy pojawić się na szczycie ProgressBar. Upewnij się, że tło jest przezroczyste, dzięki czemu można jeszcze zobaczyć ProgressBar

na przykład:

<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

Tylko ten android: layout_alignTop = "@ id/simpleProgressBar" działa dobrze dla mnie. –

8

można użyć FrameLayout aby wyświetlić TextView nad 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

Dobre rozwiązanie. Nie jestem pewien celu Układu względnego - wydaje się zbędny. – dazed

+0

@dazed FrameLayout powinien być używany do przechowywania pojedynczego widoku podrzędnego, ponieważ może być trudne zorganizowanie widoków podrzędnych w sposób, który jest skalowalny dla różnych rozmiarów ekranu bez wzajemnego nakładania się dzieci. Możesz jednak dodać wiele dzieci do FrameLayout i kontrolować ich pozycję w ramach FrameLayout, przypisując grawitację do każdego dziecka, używając atrybutu android: layout_gravity. Źródło: https://developer.android.com/reference/android/widget/FrameLayout.html W CV RelativeLayout jest lepszy, gdy chcesz umieścić wiele widoków i uporządkować je względem siebie. – androidevil

4

Najprostszym sposobem !

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

Bądź 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

to, co pracował dla mnie:

   <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

To właśnie używany do wyświetlania tekstu z postępu prac nad turbinki skupione na widoku strony internetowej:

<?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> 
Powiązane problemy