Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีทำให้สอง Activity มีสี Status Bar ต่างกันใน Android

วิธีทำให้สอง Activity มีสี Status Bar ต่างกันใน Android

มีสถานการณ์มากมายที่เราต้องการเปลี่ยนสี Action Bar ต่างกันตามความต้องการของโปรเจกต์ ตัวอย่างนี้สาธิตวิธีทำให้สอง Activity มีสี Status Bar ต่างกัน

ขั้นตอนที่ 1 - สร้างโปรเจกต์ใหม่ใน Android Studio

ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่

ขั้นตอนที่ 2 - เพิ่มโค้ดใน res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#dde4dd"
    android:gravity="center"
    android:orientation="vertical">
    <Button
        android:id="@+id/click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click for second"/>
</LinearLayout>

ในโค้ดด้านบน เราสร้างปุ่มหนึ่งตัว เมื่อคลิกจะเรียก Activity ที่สอง

ขั้นตอนที่ 3 - เพิ่มโค้ดใน src/MainActivity.java

package com.example.andy.myapplication;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.parseColor("#FFFF00"));
        }
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.click);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(i);
            }
        });
    }
}

เพื่อเปลี่ยนสี Status Bar เราใช้ setStatusBarColor() ดังนี้:

getWindow().setStatusBarColor(Color.parseColor("#FFFF00"));

ขั้นตอนที่ 4 - เพิ่มโค้ดใน res/layout/activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".Main2Activity">
</android.support.constraint.ConstraintLayout>

ขั้นตอนที่ 5 - เพิ่มโค้ดใน src/MainActivity2.java

package com.example.andy.myapplication;

import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.parseColor("#B22222"));
        }
        setContentView(R.layout.activity_main2);
    }
}

ขั้นตอนที่ 6 - เพิ่มโค้ดใน AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.andy.myapplication">
    <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"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>
    </application>
</manifest>

ลองรันแอปพลิเคชันของคุณ สมมติว่าคุณเชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์แล้ว เพื่อรันแอปจาก Android Studio ให้เปิดไฟล์กิจกรรมของโปรเจกต์หนึ่งและคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก และตรวจสอบอุปกรณ์ซึ่งจะแสดงหน้าจอเริ่มต้น

วิธีทำให้สอง Activity มีสี Status Bar ต่างกันใน Android

ในผลลัพธ์ด้านบน แสดง Activity แรกพร้อม Status Bar สีเหลือง ตอนนี้คลิกปุ่ม จะเรียก Activity ที่สองพร้อม Status Bar สีแดง ดังภาพด้านล่าง

วิธีทำให้สอง Activity มีสี Status Bar ต่างกันใน Android