2015-06-11 32 views
0

我是新的android開發者。我有問題如何將代碼轉換爲編程。我對顏色進行了編程更改。如何.xml轉換爲編程?

<stroke android:width="2dp" android:color="@color/appThemeColor"/> 

<solid android:color="@color/appBGColor" /> 

<padding 
    android:bottom="1dp" 
    android:left="1dp" 
    android:right="1dp" 
    android:top="1dp" /> 

<corners android:radius="1dp" /> 

+1

請解釋更多。 –

+0

看到這個鏈接http://stackoverflow.com/questions/7150593/android-how-to-define-shapedrawables-programmatically –

+0

我已轉換成編程式.so ican更改顏色runtime.i將從server.i獲得顏色convertthis .xml文件轉換爲java文件 – nitin

回答

0

可以通過編程恰克的顏色,你應該裏面把你的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
<stroke android:width="2dp" android:color="@color/azul_oscuro"/> 
<solid android:color="@color/azul_1" /> 
<padding 
android:bottom="1dp" 
android:left="1dp" 
android:right="1dp" 
android:top="1dp" /> 
<corners android:radius="1dp" /> 
</shape> 

然後你就可以ASING你的形狀的任何項目在佈局,例如視圖:

<View 

機器人:layout_width = 「56dp」 機器人:layout_height = 「40dp」 機器人:背景= 「@繪製/ yourshapefilename」 機器人:ID = 「@ + ID/view_color」 機器人:啓用= 「假」/>

在你的代碼,當你膨脹的佈局,你可以改變顏色是這樣的:

View myvie = findViewById(R.id.view_color); 
GradientDrawable draw = (GradientDrawable)myvie.getBackground(); 
draw.setColor(getResources().getColor(R.color.rojo)); //--> change the solid color here 
draw.setStroke(7, getResources().getColor(R.color.green)); // -->change stroke color and size 
+0

我通過編程方式更改筆觸顏色。 – nitin

+0

挖你去做嗎?因爲我不明白。任何你可以改變描邊顏色的方法:draw.setStroke(7,getResources()。getColor(R.color.green)); 查看鏈接:http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setStroke%28int,%20int%29 – venimania