2015年10月19日 星期一

Android color resource

1.create a color.xml file

2.set the color in the xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorBackground">#c213a2</color>
    <color name="colorBackground2">#c213a2</color>
    <color name="colorBackground3">#c213a2</color>
    <color name="colorBackground4">#c213a2</color>
</resources>


3.use in the layout XML code
<
<TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorBackground3"
        android:text="Hello World!" />


4. use in the hava program code



TextView tv= (TextView) findViewById(R.id.tv1);

 tv.setBackgroundColor(getResources().getColor(R.color.colorBackground2));