Android > resource - »ö»ó ¸®¼Ò½º <color>
µî·ÏÀÏ : 2017-07-05 16:55
Á¶È¸¼ö : 53,259
»ö»ó ¸®¼Ò½º´Â º¸Åë /res/values/colors.xml ¿¡¼ °ü¸®ÇÑ´Ù.
colors.xml ÆÄÀÏÀ» »õ·Î ¸¸µé°í, »ö»ó <color>¸¦ »õ·Î µî·ÏÇÑ ÈÄ, xml°ú java¿¡¼ È£ÃâÇÏ¿© º¸ÀÚ.
1. res(¸®¼Ò½º)¿¡ <color> µî·ÏÇϱâ
/res/values/ ¹Ø¿¡ colors.xml ÆÄÀÏÀ» »õ·Î ¸¸µç´Ù.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color_red">#FF2222</color>
</resources>
- nameÀ» "color_red" ·Î <color> µî·Ï
- »ö»óÀº #FF2222 (»¡°) À¸·Î µî·Ï
2. layoutÀÇ xml¿¡¼ ¸®¼Ò½º <color> °¡Á®¿À±â
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sample1"
android:textColor="@color/color_red" />
</RelativeLayout>
- id : TextView01 ·Î TextView µî·Ï
- ¹®ÀÚ¿Àº sample1 À̸§À¸·Î µî·ÏµÈ string ¸®¼Ò½º µî·Ï
- "color_red" ·Î µî·ÏµÈ <color> ¸®¼Ò½º¸¦ ¹®ÀÚ¿ »ö±ò·Î ÁöÁ¤ ( android:textColor="@color/color_red" )
½ÇÇàȸéÀ» º¸ÀÚ!
- String ¸®¼Ò½º sample1ÀÇ ¹®ÀÚ¿ Hello~! Friends~! ¿¡ »¡°£»ö Àû¿ë
3. Activity.java¿¡¼ <color> ¸®¼Ò½º °¡Á®¿À±â
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// "color_red" ¶ó´Â À̸§ÀÇ <color> ¸®¼Ò½º¸¦ °¡Á®¿Â´Ù. int °ª.
int colorRed = getResources().getColor(R.color.color_red);
// id : TextView01 <TextView> ¸¦ °¡Á®¿Â´Ù.
TextView textView01 = (TextView)findViewById(R.id.TextView01);
// TextView01 ¿¡ colorRed¸¦ Àû¿ëÇÑ´Ù. (»¡°»ö)
textView01.setTextColor(colorRed);
}
}
- getResources().getColor() ·Î ¸®¼Ò½º¿¡ µî·ÏÇÑ <color> ¸¦ °¡Á®¿Â´Ù.
- TextView¸¦ È£ÃâÇÑ ÈÄ, setTextColor ·Î °¡Á®¿Â <color> ¸¦ Àû¿ëÇÑ´Ù.
½ÇÇàȸéÀ» º¸ÀÚ!
- TextView01¿¡ »¡°»öÀÌ Àû¿ëµÇ¾î Àִµ¥¿¡, ´Ù½Ã »¡°»öÀ» Àû¿ëÇÏ¿© °á°ú´Â °°´Ù.