2012-05-03 33 views
4

我需要以編程方式找出哪些可繪製的Android默認情況下使用渲染ProgressBar。由於這取決於正在使用的主題,所以我嘗試了以下幾行:在運行時從Android主題獲取默認屬性值?

TypedArray progressBarAttrs = context.getTheme().obtainStyledAttributes(
      new int[] { android.R.attr.indeterminateDrawable }); 
    progressDrawableId = progressBarAttrs.getResourceId(0, -1); 

但是這總是返回-1。

這是因爲android.R.attr.indeterminateDrawable沒有聲明樣式或什麼?有沒有其他方法可以達到同樣的效果?

回答

0

以下收益爲我繪製(試驗和錯誤,我當然不知道如何obtainStyledAttributes正好解決):

int[] attrs = new int[] { android.R.attr.indeterminateDrawable /* index 0 */}; 
TypedArray ta = context.obtainStyledAttributes(android.R.style.Widget_ProgressBar, attrs); 
// .... 
ta.recycle(); 

然而,分配是繪製一個進度條時:

progressBar.setIndeterminateDrawable(ta.getDrawable(0)); 

結果看起來不像默認的ProgressBar不確定微調器。這是Android的微調,但更大更厚。

我現在知道爲什麼,但千萬注意ProgressBar.java在Android中,檢索繪製資源後,做了它:

drawable = tileifyIndeterminate(drawable); 
setIndeterminateDrawable(drawable); 
0

快樂,給你更多的幫助,週一如果你需要它(只是出頭,並注意到你的問題),

但檢查出Hackbods在這裏回答https://stackoverflow.com/a/2130528/726954和她的鏈接到示例項目,這將允許你從一個主題拉回樣式資源。