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

วิธีทำพื้นหลังโปร่งใส 20% ใน Android

การกำหนดความโปร่งใส (Alpha) ของสีใน Android

ใน Android สีสามารถกำหนดในรูปแบบ ARGB (Alpha, Red, Green, Blue) โดยค่า Alpha ควบคุมความโปร่งใส:

  • 00 = โปร่งใส 100% (มองไม่เห็น)
  • FF = ไม่โปร่งใส 0% (ทึบเต็มที่)

ตารางค่า Alpha แบบเฮกซาเดซิมอล (Hex)

ความโปร่งใสค่า Hex
100% (ทึบ)FF
90%E6
80%CC
70%B3
60%99
50%80
40%66
30%4D
20%33
10%1A
0% (โปร่งใส)00

ตัวอย่าง: สีเหลือง (#FFFF00) โปร่งใส 20%

รูปแบบ: #AARRGGBB → #33FFFF00

ขั้นตอนการทำงาน

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

ไปที่ File → New Project ใน Android Studio

ขั้นตอนที่ 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="#33FFFF00"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:id="@+id/text"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

กำหนด android:background="#33FFFF00" ให้ LinearLayout (พื้นหลังสีเหลืองโปร่งใส 20%)

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

package com.example.andy.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.text);
        textView.setText("Tutorialspoint india pvt ltd");
    }
}

การทดสอบ

รันแอปพลิเคชัน จะเห็นพื้นหลังสีเหลืองโปร่งใส 20% (ไม่ใช่สีเหลืองทึบเต็มที่)