2

我想在一個標準的按鈕中間創建一個進度條,這樣的事情: progressbar進度在按鈕的中間

在XML佈局,當我點擊的進度,我做看到它位於我希望的位置,但實時,我看不到它,它感覺就像隱藏在按鈕下面。 android studio xml

我已經試過:

  1. 更改視圖的定位(鈕高於/低於進度)
  2. 不確定
  3. 與進度的風格演奏

似乎沒有什麼是工作。

這裏是我的xml:

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

    <Button 
     android:id="@+id/button_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:elevation="1dp" 
     android:text="test button" /> 

    <ProgressBar 
     android:id="@+id/progress" 
     style="?android:attr/progressBarStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/button_id" 
     android:layout_alignTop="@+id/button_id" 
     android:layout_centerHorizontal="true" 
     android:indeterminate="true" 
     android:indeterminateTint="@android:color/holo_blue_dark" 
     android:visibility="visible" /> 

</RelativeLayout> 
+0

也許是因爲你需要前更改進度條 – has19

+0

的顏色嘗試了不同的顏色,它不是。 – Dus

+0

將這些添加到您的主題庫樣式中#ffff4444 #ff0099cc並再試一次 – has19

回答

6

該問題與elevation有關。添加elevation的進度

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

    <Button 
     android:id="@+id/button_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:elevation="1dp" 
     android:text="test button" /> 

    <ProgressBar 
     android:id="@+id/progress" 
     style="?android:attr/progressBarStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/button_id" 
     android:layout_alignTop="@+id/button_id" 
     android:layout_centerHorizontal="true" 
     android:indeterminate="true" 
     android:elevation="2dp" 
     android:indeterminateTint="@android:color/holo_blue_dark" 
     android:visibility="visible" /> 

</RelativeLayout> 
+1

完美。忘了那個。只需在進度條中添加高程就可以實現。甚至認爲海拔僅來自api 21. – Dus

+1

爲什麼海拔問題?立面解決問題。謝謝 –

0

在預覽中,進度始終是無形的(至少對我來說)。

問題是,您將高程應用於按鈕。這會更改視圖的z級別。所以ProgressBar真的在你的按鈕後面,因爲ProgressBar沒有提升。

給ProgressBar提升3dp對我有用。

<ProgressBar 
       android:id="@+id/progress" 
       style="?android:attr/progressBarStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBottom="@+id/button_id" 
       android:layout_alignTop="@+id/button_id" 
       android:layout_centerHorizontal="true" 
       android:indeterminate="true" 
       android:elevation="3dp" 
       android:indeterminateTint="@android:color/holo_blue_dark" 
       android:visibility="visible" /> 

編輯: 你會更好,使用ProgressDialog

ProgressDialog dialog = new ProgressDialog(this); 
dialog.setMessage("Wait..."); 
dialog.show(); 
+0

爲什麼我應該使用progressdialog,如果我只能在xml中優雅地做到這一點,而沒有任何代碼背後呢? – Dus

+0

,因爲我覺得它「非常優雅」。將ProgressBar設置爲可見/不可見不會比代表ProgressDialog的代碼少得多。 – AlbAtNf