2012-12-05 71 views
61

目前,我正在使用WebView或TextView在一個應用程序中顯示來自webservice的一些動態數據。 如果數據包含純文本,它將使用TextView並應用styles.xml中的樣式。 如果數據包含HTML(主要是文本和圖像),則使用WebView。如何從styles.xml以編程方式檢索樣式屬性

但是,這個WebView是無風格的。因此它看起來與通常的TextView有很大不同。 我讀過,可以通過簡單地將一些HTML直接插入到數據中來設置WebView中的文本樣式。這聽起來很容易,但我希望將Styles.xml中的數據用作此HTML中所需的值,因此如果更改樣式,我不需要在兩個位置更改顏色等等。

那麼,我將如何能夠做到這一點?我已經做了大量的搜索,但是我沒有找到從styles.xml中實際檢索不同樣式屬性的方法。我在這裏錯過了什麼,或者是否真的無法檢索這些值?

的風格,我想從數據如下:

<style name="font4"> 
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:textSize">14sp</item> 
    <item name="android:textColor">#E3691B</item> 
    <item name="android:paddingLeft">5dp</item> 
    <item name="android:paddingRight">10dp</item> 
    <item name="android:layout_marginTop">10dp</item> 
    <item name="android:textStyle">bold</item> 
</style> 

我主要感興趣的TEXTSIZE和文字顏色。

+0

爲什麼你不解析XML(例如Android的SAX解析器)? – m0skit0

回答

128

以編程方式從styles.xml檢索自定義樣式是可能的。

styles.xml定義一些任意的方式:

<style name="MyCustomStyle"> 
    <item name="android:textColor">#efefef</item> 
    <item name="android:background">#ffffff</item> 
    <item name="android:text">This is my text</item> 
</style> 

現在,檢索的樣式像這樣

// The attributes you want retrieved 
int[] attrs = {android.R.attr.textColor, android.R.attr.background, android.R.attr.text}; 

// Parse MyCustomStyle, using Context.obtainStyledAttributes() 
TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, attrs); 

// Fetch the text from your style like this.  
String text = ta.getString(2); 

// Fetching the colors defined in your style 
int textColor = ta.getColor(0, Color.BLACK); 
int backgroundColor = ta.getColor(1, Color.BLACK); 

// Do some logging to see if we have retrieved correct values 
Log.i("Retrieved text:", text); 
Log.i("Retrieved textColor as hex:", Integer.toHexString(textColor)); 
Log.i("Retrieved background as hex:", Integer.toHexString(backgroundColor)); 

// OH, and don't forget to recycle the TypedArray 
ta.recycle() 
+0

令人敬畏的工作伴侶。在被告知(並且閱讀了很多)之後,它無法完成,你已經完全顯示它可以。試過了,我可以從我的風格中獲得字體顏色。不能完全獲得文本大小,但我會弄清楚。只要我可以發送獎金(從現在起18小時),100分即將到來。非常感謝! –

+4

是否也可以在運行時更改樣式中定義的值? – ekjyot

+0

如果我想更改檢索的屬性的顏色,該怎麼辦?就像在這段代碼中動態改變背景或文字顏色一樣? – MSIslam

28

答案@Ole給似乎使用某些屬性,如則shadowColor何時打破, shadowDx,shadowDy,shadowRadius(這些只是我發現的幾個,可能還有更多)

我不知道爲什麼爲什麼出現這個問題,我在問關於here,但@AntoineMarques編碼風格似乎解決了這個問題。

爲了使這項工作對任何屬性都將是這樣的


首先,定義一個風格化包含像這樣

attrs.xml

<resources>  
    <declare-styleable name="MyStyle" > 
     <attr name="android:textColor" /> 
     <attr name="android:background" /> 
     <attr name="android:text" /> 
    </declare-styleable> 
</resources> 

然後在代碼中的資源ID你會這樣做來獲取文本。

TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, R.styleable.MyStyle); 
String text = ta.getString(R.styleable.MyStyle_android_text); 

使用此方法的優點是,您正在按名稱檢索值而不是索引。

+3

這應該是被接受的答案,因爲Ole的答案爲我生成了「預期的類型風格資源」警告。 – thecoolmacdude

+1

適用於我,但對於Android Studio,我需要在前綴中加上@SuppressWarnings(「ResourceType」)。 – eruve

+2

這個答案應該是公認的答案。這是對我所有的屬性的工作,而投票最多的人浪費了我很多時間。 – jerry

0

如果接受的解決方案不是試圖重命名attr.xml到attrs.xml工作(工作對我來說)

相關問題