Tạo màu nền bán trong suốt với Hex Alpha
Trong Android, màu định dạng ARGB: #AARRGGBB. 2 chữ số đầu (AA) là độ trong suốt (Alpha): 00 (hoàn toàn trong) đến FF (đặc).
Bảng tra nhanh % 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 |
Ví dụ: Vàng (#FFFF00) trong suốt 20%
Alpha 20% = 33. Màu kết quả: #33FFFF00.
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#33FFFF00"> <!-- 20% transparent yellow -->
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.text);
tv.setText("Tutorialspoint India Pvt Ltd");
}
}
Cách làm trong Code (Java/Kotlin)
// Java
int color = Color.argb(51, 255, 255, 0); // 51/255 ≈ 20%
view.setBackgroundColor(color);
// Kotlin
view.setBackgroundColor(Color.argb(51, 255, 255, 0))