Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Hiển thị Dialog toàn màn hình (Fullscreen) Android Kotlin

Hướng dẫn này minh họa cách hiển thị Dialog toàn màn hình (fullscreen) trong Android sử dụng Kotlin.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:onClick="clickHere"
        android:text="Click here" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World"
        android:textSize="24sp"
        android:textStyle="bold|italic"
        android:textColor="@android:color/holo_purple" />

</RelativeLayout>

Bước 3: Viết code MainActivity.kt

import android.app.Dialog
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }

    fun clickHere(view: View) {
        val dialog = Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen)
        dialog.setContentView(R.layout.activity_main)
        dialog.show()
    }
}

Bước 4: Cấu hình AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.q11">

    <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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Nhấn nút "Click here" → Dialog toàn màn hình hiện ra, che phủ status bar và navigation bar, sử dụng lại layout activity_main.

Các Theme Dialog fullscreen có sẵn

ThemeĐặc điểm
android.R.style.Theme_Black_NoTitleBar_FullscreenĐen, không title bar, fullscreen
android.R.style.Theme_Holo_Light_NoActionBar_FullscreenTrắng (Holo), không action bar
android.R.style.Theme_Material_Light_NoActionBar_FullscreenMaterial Light, không action bar
Theme.MaterialComponents.Light.Dialog.FullscreenMaterial Components, fullscreen

Tùy chỉnh Dialog fullscreen hiện đại (Recommend)

val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(R.layout.custom_dialog_layout)
dialog.window?.apply {
    setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    setDimAmount(0.5f) // Độ mờ background
}
dialog.show()

BottomSheetDialogFragment (Thay thế Dialog fullscreen)

Tốt hơn cho UX hiện đại - trượt lên từ dưới:

class MyBottomSheet : BottomSheetDialogFragment() {
    override fun getLayoutRes() = R.layout.bottom_sheet_layout
    override fun onStart() {
        super.onStart()
        dialog?.behavior?.state = BottomSheetBehavior.STATE_EXPANDED
    }
}