ซึ่งปกติแล้วเวลาเราใส่สีตัวอักษรหรือพื้นหลังจะใส่แบบตัวต่อตัว เช่น
android:textColor=
"#โค้ดสี"
ตัวอย่าโค้ด
โค้ด: เลือกทั้งหมด
<LinearLayout 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"
tools:context="com.example.worachat.radiobutton.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="50dp"
android:textColor="#9ACD32"/></LinearLayout>
- color1.png (47.44 KiB) Viewed 1019 times
ซึ่งในบล็อกนี้ผมจะมาสร้างการใช้สีใน Values โดยเราจะเข้าไป /res/values/colors.xml
- color-2.png (30.59 KiB) Viewed 1029 times
<color name="ชื่อสีที่ใส่(ควรเป็นภาษาอังกฤษ)">#โค้ดสี</color>
ตัวอย่าโค้ด
โค้ด: เลือกทั้งหมด
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="YellowGreen">#9ACD32</color>
</resources>
หากเราต้องการใช้สีที่เราสร้างไว้ ก็สามารถเรียกใช้ได้เลยทั้งพื้นหลังและตัวอักษร เช่น
android:background=
"@color/YellowGreen"
ตัวอย่าโค้ด
โค้ด: เลือกทั้งหมด
<LinearLayout 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"
tools:context="com.example.worachat.radiobutton.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="50dp"
android:textcolor="@color/YellowGreen"/></LinearLayout>
ผลลัพธ์จะเหมือนกับภาพแรก โค้ดจะแตกต่างกัน แต่หากเราสร้างใน Values จะนำไปใช้งานได้ง่ายและสะดวกกว่ามากครับ
- color1.png (47.44 KiB) Viewed 1019 times