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

Cách thay đổi vị trí hiển thị Dialog trên màn hình Android

Hướng dẫn này minh họa cách điều chỉnh vị trí hiển thị của AlertDialog (ví dụ: góc dưới cùng màn hình) bằng WindowManager.LayoutParams và Gravity.

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

Mở Android Studio, chọn File → New Project.

Bước 2: Giao diện trống (res/layout/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">
</RelativeLayout>

Bước 3: Tạo và định vị Dialog (src/MainActivity.java)

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Thông báo");
        builder.setMessage("Chúc bạn một ngày tốt lành!");
        builder.setCancelable(false);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Xử lý OK
            }
        }).setNegativeButton("Hủy", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Xử lý Hủy
            }
        });

        final AlertDialog alertDialog = builder.create();
        alertDialog.show();

        // QUAN TRỌNG: Đặt vị trí SAU khi gọi show()
        alertDialog.getWindow().setGravity(Gravity.BOTTOM);
        
        // Tùy chọn: thêm margin bottom
        WindowManager.LayoutParams params = alertDialog.getWindow().getAttributes();
        params.y = 100; // Khoảng cách từ đáy màn hình (pixel)
        alertDialog.getWindow().setAttributes(params);
    }
}

Các giá trị Gravity phổ biến

Hằng sốVị trí
Gravity.CENTERGiữa màn hình (mặc định)
Gravity.TOPĐầu màn hình
Gravity.BOTTOMĐáy màn hình
Gravity.LEFT | Gravity.CENTER_VERTICALBên trái, giữa dọc
Gravity.RIGHT | Gravity.CENTER_VERTICALBên phải, giữa dọc

Bước 4: Manifest (androidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Dialog sẽ xuất hiện ở dưới cùng màn hình thay vì giữa màn hình.