2014-01-09 16 views
2

我的Android應用程序中有一個帶有顏色的xml文件。 我可以在代碼中選擇其中一種嗎?我可以在Android中使用不同顏色的xml文件嗎?

喜歡的東西:

(if value == 0) { 
    // I choose the file 'a' 
} else { 
    // I choose the file 'b' 
} 

這可能嗎?

提前致謝。

+0

是的,膨脹你需要的文件。 – Nfear

+0

我的解決方案是使用主題。謝謝大家。 –

回答

0

您可以在一個單一的資源文件中使用不同的爲你的顏色,CF http://developer.android.com/guide/topics/resources/more-resources.html#Color

例子:

XML

<resources> 
    <color 
    name="color_a">hex_color</color> 
    <color 
    name="color_b">hex_color</color> 
</resources> 

JAVA

if (value == 0) color = R.color.color_a; 
else color = R.color.color_b; 
+0

我的解決方案是使用主題。謝謝大家。 –

3

你可以這樣說:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v; 
if(value == 0){ 
    v = inflater.inflate(R.layout.file_yellow, null); 
}else{ 
    v = inflater.inflate(R.layout.file_green, null); 
} 
// do stuff with view 
+0

是你的編輯答案現在好了。在你犯了一個小錯誤之前。 +1爲你 –

+0

是的,我已經這樣做。因爲在之前你做錯了我的朋友。我也爲你的答案+1。儘管你已經編輯了你的答案,但我刪除了我的答案。不要擔心老兄 –

+0

我提交後立即注意到它沒關係;) – Nfear

相關問題