我需要在運行時更改應用程序的顏色。我分析的數據文件,以獲得顏色和我與靜態字段和方法保存在類:以編程方式更改可繪製xml文件中的顏色
public class Colors {
private static String colorOneBackground = "#00577F";
private static String colorOneForeground = "#FFFFFF";
public static void setColorOneBackground(String colorOneBackground) {
Colors.colorOneBackground = colorOneBackground;
}
public static int getColorOneForeground() {
return Color.parseColor(colorOneForeground);
}
// more colors...
然後,例如,當我想改變屏幕的背景我願意這樣做:
RelativeLayout relativeLayout = (RelativeLayout) myView.findViewById(R.id.loginBackground);
relativeLayout.setBackgroundColor(Colors.getColorOneBackground());
與文字瀏覽和其他小工具一樣。但是,我遇到了一個問題。有些款式在可繪製文件夾中定義,例如, mybutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" http://schemas.android.com/apk/res/android "
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:centerColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="5dp" />
<stroke android:width="3px" android:color="#000000" />
</shape>
而且我設置爲我的按鈕的背景:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:background="@drawable/mybutton" />
正如我說我要以編程方式更改這些顏色值。所以我想知道是否有可能在xml文件中定義顏色值?
使用** setBackGroundRes()方法**。 –
請再次閱讀我的問題。我想改變xml文件中的顏色值,我知道如何在代碼中設置可繪製背景 –
http://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically – amorenew