[英]Android - displaying text in center of progress bar
我已經制作了一個水平進度條,效果很好。 我想在它的中間顯示一個 textview 或類似的東西,在欄加載時顯示倒計時。 請記住,它不是進度對話框,進度條位於活動內並顯示倒數計時器。 任何人都可以幫助我,最好的方法是什么,我怎樣才能做到這一點?
如果您的ProgressBar
和TextView
在RelativeLayout
您可以給ProgressBar
一個 id,然后使用它將TextView
與ProgressBar
對齊。 然后它應該顯示在ProgressBar
之上。 確保背景是透明的,以便您仍然可以看到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>
您可以使用 FrameLayout 在 ProgressBar 上顯示 TextView:
...
<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>
您可以在其中設置帶有RelativeLayout 的LinearLayout並使 RelativeLayout高度和寬度等於wrap_content然后在 ProgressBar 下的 TextView 您將添加 android: layout_centerInParent="true"
例子 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="200dp"
android:layout_height="200dp"
android:max="100"
android:progress="65" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:textColor="@color/white"
android:text="6:00 AM"
android:textSize="25sp"
android:textStyle="bold"/>
</RelativeLayout>
</LinearLayout>
這對我有用:
<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>
這是我用來在以 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>
您可以將 ProgressBar 和 TextView 放在相對布局中,並將以下行添加到這兩個子項的 XML 文件中:
android:layout_centerInParent="true"
這應該強制 ProgressBar 和 TextView 都顯示在同一相對布局的中心。
如果您想在 Circular ProgressBar 中顯示 Textfield,這也可以工作。
附注。 如果您還想在進度條的上方或下方(垂直)顯示 textview,則可以調整 TextView 的“邊距”元素。
在這里,我使用框架布局並將文本放入框架布局
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
map:layout_constraintBottom_toBottomOf="@+id/programStatusTextView"
map:layout_constraintStart_toEndOf="@+id/programStatusTextView"
map:layout_constraintTop_toTopOf="@+id/programStatusTextView">
<RelativeLayout
android:layout_width="130dp"
android:layout_height="60dp"
android:background="@drawable/battery">
<ProgressBar
android:id="@+id/batteryIndicaterBar"
style="@style/CustomProgressBarHorizontal"
android:layout_width="119dp"
android:layout_height="52dp"
android:layout_marginStart="3dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="7dp"
android:backgroundTint="@color/colorPrimary"
android:max="100"
android:minHeight="10dip"
android:paddingEnd="4dp"
android:progress="0"
android:progressBackgroundTintMode="src_over"
android:progressTint="@color/green"
android:visibility="visible" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textSize="20sp"
android:textStyle="bold" />
</FrameLayout>
這是我得到的輸出
在 Activity 中,我們可以使用以下方法顯示和關閉ProgressBar
// Method to show Progress bar
private void showProgressDialogWithTitle(String substring) {
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//Without this user can hide loader by tapping outside screen
progressDialog.setCancelable(false);
progressDialog.setMessage(substring);
progressDialog.show();
}
// Method to hide/ dismiss Progress bar
private void hideProgressDialogWithTitle() {
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.dismiss();
}
檢查其他
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.