ภาพรวม
ตัวอย่างนี้แสดงวิธีเปลี่ยนสีพื้นหลังของ ActionBar (App Bar) ใน Android โดยการกำหนดค่า colorPrimary ใน Style/Theme
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็นเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ขั้นตอนที่ 4: กำหนด Style (res/values/styles.xml)
<resources>
<!-- Theme หลัก -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- สีพื้นหลัง ActionBar -->
<item name="colorPrimary">#995544</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Style เพิ่มเติมสำหรับ Title Text -->
<style name="ActionBar.Solid.Ribbit.TitleTextStyle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#976325</item>
</style>
</resources>
ขั้นตอนที่ 5: กำหนดค่า Manifest (Manifests/AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
การปรับแต่งเพิ่มเติม (Programmatically)
สามารถเปลี่ยนสี ActionBar ในโค้ด Java ได้ด้วย:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ใช้ ActionBar แบบเดิม (AppCompat)
if (getSupportActionBar() != null) {
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#995544")));
// หรือเปลี่ยนสีข้อความ Title
getSupportActionBar().setTitle(Html.fromHtml("<font color='#976325'>My Title</font>"));
}
}
การทดสอบ
เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run - ActionBar จะมีสีพื้นหลัง #995544 (สีน้ำตาลแดง)