2013-08-23 66 views
24

Tôi đã tạo thanh tiến trình nằm ngang và nó hoạt động hoàn hảo. Tôi muốn hiển thị một textview hoặc một cái gì đó tương tự ngay ở giữa của nó cho thấy một đếm ngược như thanh đang tải. Hãy ghi nhớ hộp thoại không phải là tiến trình của nó, thanh tiến trình nằm bên trong một hoạt động và hiển thị đồng hồ đếm ngược. Bất cứ ai có thể giúp tôi ra, whats cách tốt nhất để làm điều này và làm thế nào tôi có thể thực hiện điều này?Android - hiển thị văn bản ở giữa thanh tiến trình

Trả lời

33

Nếu ProgressBar của bạn và TextView được bên trong một RelativeLayout bạn có thể cung cấp cho các ProgressBar một id, và sau đó sắp xếp các TextView với ProgressBar sử dụng đó. Sau đó nó sẽ hiển thị trên đầu trang của ProgressBar. Hãy chắc chắn rằng nền trong suốt để bạn vẫn có thể thấy ProgressBar

Ví dụ:

<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

Chỉ android này: layout_alignTop = "@ id/simpleProgressBar" hoạt động tốt cho tôi. –

8

Bạn có thể sử dụng một FrameLayout để hiển thị TextView trong 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

Giải pháp tốt. Không chắc chắn về mục đích của Bố cục tương đối - dường như không cần thiết. – dazed

+0

@ Frame FrameLayout nên được sử dụng để giữ một chế độ xem con, bởi vì có thể khó tổ chức chế độ xem con theo cách có thể mở rộng với các kích thước màn hình khác nhau mà không có trẻ chồng chéo nhau. Tuy nhiên, bạn có thể thêm nhiều trẻ em vào FrameLayout và kiểm soát vị trí của chúng trong FrameLayout bằng cách gán trọng số cho mỗi đứa trẻ, sử dụng thuộc tính android: layout_gravity. Nguồn: https://developer.android.com/reference/android/widget/FrameLayout.html Trong lý lịch, RelativeLayout sẽ tốt hơn khi bạn muốn đặt nhiều chế độ xem và sắp xếp chúng tương đối với nhau. – androidevil

4

Cách đơn giản nhất !

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

Hãy 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

đó là những gì làm việc cho tôi:

   <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

Đây là những gì tôi đã sử dụng để hiển thị văn bản tiến bộ trên một spinner tập trung vào một cái nhìn 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> 
Các vấn đề liên quan