TIP การสร้า color ใน Values เพื่อนำไปใช้งานได้ง่ายๆ

Mobile Application Developing- Android, iOS, Window Phone สอนเขียนโปรแกรมบนมือถือ ระบบปฏิบัติการต่าง แอนดรอยด์ ไอโอเอส วินโดโฟน สอนเขียนโปรแกรมบนมือถือ

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
worachat
PHP Hero Member
PHP Hero Member
โพสต์: 179
ลงทะเบียนเมื่อ: 23/05/2016 9:55 am

TIP การสร้า color ใน Values เพื่อนำไปใช้งานได้ง่ายๆ

โพสต์ที่ยังไม่ได้อ่าน โดย worachat »

ซึ่งปกติแล้วเวลาเราใส่สีตัวอักษรหรือพื้นหลังจะใส่แบบตัวต่อตัว เช่น
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
color1.png (47.44 KiB) Viewed 1011 times
ซึ่งในบล็อกนี้ผมจะมาสร้างการใช้สีใน Values โดยเราจะเข้าไป /res/values/colors.xml
color-2.png
color-2.png (30.59 KiB) Viewed 1021 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
color1.png (47.44 KiB) Viewed 1011 times
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 32