2013-08-30 119 views
27

我試圖在我的框架頂部放置水平進度條。但似乎有填充,我根本無法刪除。任何幫助?將ProgressBar水平對齊(刪除間隙)

enter image description here

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
<ProgressBar android:id="@+id/flv_progress_bar" 
      android:layout_width="fill_parent" 
      style="@android:style/Widget.Holo.ProgressBar.Horizontal" 
      android:indeterminateOnly="true" 
      android:layout_height="wrap_content" 
      android:layout_gravity="top"/> 
</FrameLayout> 

回答

32

Pork'n'Bunny的答案工作API級別16和設備。該解決方案適用於所有設備。 只需在XML中將其提升6dp即可。

<ProgressBar 
     android:id="@+id/progressbar_Horizontal" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:max="100" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="-6dp" /> 
+16

這對於默認樣式的更改有多強大?他們是否保證在所有的永恆中看起來和工作都一樣,或者他們在理論上可以隨時用替代它們的東西來替代它們,這會導致問題(例如看不見的進度條)。 – Joey

+6

這不適用於所有設備。在Galaxy S6上,即使使用'-6dp'頂部邊距,進度條上方的空白仍然可見。 –

4

從外觀上來看,我認爲它引起的默認樣式中使用的圖像

style="@android:style/Widget.Holo.ProgressBar.Horizontal" 

(圖像,我指的是這個:@android:drawable/progress_indeterminate_horizontal_holo@android:drawable/progress_horizontal_holo_dark

這些圖像有很多畫布背景;因此它顯示出這種差距。

我的推薦:

使用自定義圖像來設計你的PregressBar。一個沒有太多畫布間距的畫布。

enter image description here

+2

這是最終的正確答案。我將不得不編輯全部可繪製(9個補丁)的版本來擺脫填充。 –

-1

我認爲這個問題是高度屬性。嘗試將佈局高度設置爲某個值

<ProgressBar 
      android:id="@+id/progressBar" 
      android:max="100" 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      style="@android:style/Widget.ProgressBar.Horizontal" 
      /> 
+0

這不起作用。 –

+0

如果是這種情況,那麼你應該改變對可繪製那些定製在您的樣式文件 <樣式名稱=「Widget.Holo.ProgressBar.Horizo​​ntal」父=「Widget.ProgressBar.Horizo​​ntal」> <項目名稱= 「機器人:progressDrawable」>定製抽拉 <項目名= 「機器人:indeterminateDrawable」>定製抽拉 <項目名= 「機器人:layout_height」> 16dip <項目名= 「機器人:maxHeight」> 16dip – Raghuram

3

此問題的完整解決方案如下。以防萬一某人需要代碼片段,這就是我所做的。

  1. 複製所有8不確定的水平進度繪項目
  2. 編輯使用一些像機械手可繪製並刪除不必要的墊襯
  3. 複製從Android平臺
  4. 複製的風格插件的繪製名爲progress_indeterminate_horizo​​ntal_holo.xml XML。 ProgressBar.Horizo​​ntal及其父母
  5. 在佈局中手動設置樣式和min_height

這裏是progress_indeterminate_horizo​​ntal_holo.xml複製到我的地方風格文件

<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
<item android:drawable="@drawable/progressbar_indeterminate_holo1" android:duration="50" /> 
<item android:drawable="@drawable/progressbar_indeterminate_holo2" android:duration="50" /> 
<item android:drawable="@drawable/progressbar_indeterminate_holo3" android:duration="50" /> 
<item android:drawable="@drawable/progressbar_indeterminate_holo4" android:duration="50" /> 
<item android:drawable="@drawable/progressbar_indeterminate_holo5" android:duration="50" /> 
<item android:drawable="@drawable/progressbar_indeterminate_holo6" android:duration="50" /> 
<item android:drawable="@drawable/progressbar_indeterminate_holo7" android:duration="50" /> 
<item android:drawable="@drawable/progressbar_indeterminate_holo8" android:duration="50" /> 

樣式資源。

<style name="Widget"> 
    <item name="android:textAppearance">@android:attr/textAppearance</item> 
</style> 

<style name="Widget.ProgressBar"> 
    <item name="android:indeterminateOnly">true</item> 
    <item name="android:indeterminateBehavior">repeat</item> 
    <item name="android:indeterminateDuration">3500</item> 
</style> 

<style name="Widget.ProgressBar.Horizontal"> 
    <item name="android:indeterminateOnly">false</item> 
    <item name="android:indeterminateDrawable">@drawable/progress_indeterminate_horizontal_holo</item> 
</style> 

最後,在我的本地佈局文件中設置最小高度爲4dp。

<ProgressBar 
    android:id="@+id/pb_loading" 
    style="@style/Widget.ProgressBar.Horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:indeterminate="true" 
    android:minHeight="4dp" 
    android:minWidth="48dp" 
    android:progressDrawable="@drawable/progress_indeterminate_horizontal_holo" /> 
+0

謝謝Subin!我還沒有真正着手這麼做,因爲我不確定除了編輯一張可繪製的圖片之外,還有多少錢我們都知道。感謝分享。 –

+0

問題是關於'?android:attr/progressBarStyleHorizo​​ntal'而不是'@ style/Widget.ProgressBar.Horizo​​ntal' –

1

因爲drawable在屏幕截圖中按照預期工作,所以想出了另一種解決方案。只需將它推高6dp即可。

mProgress = (ProgressBar) view.findViewById(R.id.fwv_progress); 
    mProgress.animate().withLayer().yBy(-6.0f*getResources().getDisplayMetrics().density).setDuration(0).start(); 
+0

它只能運行API級別16以上。有沒有什麼辦法可以修復API 8及以上版本? – bCliks