我是android開發新手,目前正在製作計算器應用程序。其中,我的活動有一些按鈕,我應用了相同的風格。代碼的結構是一樣的東西,如下以編程方式更改style.xml中item的值android
layout.xml <LinearLayout> <Button android:text="Button 1" style="@style/styleName" > <Button android:text="Button 2" style="@style/styleName" > <Button android:text="Button 3" style="@style/styleName" > </LinearLayout>
style.xml
<resources> <style name="styleName"> <item name="attribute1">attribute1Value</item> <item name="attribute2">attribute2Value</item> <item name="attribute3">attribute3Value</item> <item name="android:background">BackgroundValue</item> </style> </resources>
現在我想提供一個功能,用戶在其中時,他/她從設置中選擇一個特定的主題,所有按鈕的背景將改變。如果我可以在styleName中更改背景顏色的值,就可以實現這一點。我不知道如何實現這一點。我環顧網絡,並沒有找到任何令人滿意的答案來實現相同的目標。
http://stackoverflow.com/questions/13719103/how-to-retrieve-style-attributes-programatically-from-styles-xml你試試這個? – Amsheer