溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android如何自定義對話框

發(fā)布時間:2021-12-30 11:03:59 來源:億速云 閱讀:115 作者:小新 欄目:開發(fā)技術

這篇文章給大家分享的是有關android如何自定義對話框的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1.實現(xiàn)效果

  android如何自定義對話框

2.定義dialog.xml (res/layout/dialog.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <RelativeLayout
        android:layout_width="250sp"
        android:layout_height="270sp"
        android:layout_centerInParent="true">
 
        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="35sp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15sp"
            android:textColor="#333333"
            android:textSize="17sp" />
 
        <TextView
            android:id="@+id/dialog_message"
            android:layout_width="wrap_content"
            android:layout_height="160sp"
            android:layout_centerInParent="true"
            android:layout_marginTop="65sp"
            android:textColor="#333333"
            android:textSize="17sp" />
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30sp"
            android:layout_marginBottom="20sp"
            android:orientation="horizontal">
 
            <LinearLayout
                android:id="@+id/dialog_confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
 
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/confirm_button_style"
                    android:gravity="center"
                    android:text="確定"
                    android:textColor="@color/white"
                    android:textSize="18sp" />
            </LinearLayout>
 
            <LinearLayout
                android:id="@+id/dialog_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="22sp">
 
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cancel_button_style"
                    android:gravity="center"
                    android:text="取消"
                    android:textColor="@color/teal_200"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
 
    </RelativeLayout>
</RelativeLayout>

3. 設置確定、取消按鈕的background

上文的dialog.xml中,確定和取消按鈕都是TextView,所以需要自定義按鈕的背景

confirm_button_style.xml  (所有的color需要自定義)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="1000sp"/>
    <solid android:color="@color/teal_200"/>
    <stroke
        android:width="0.5sp"
        android:color="@color/colorAccent"/>
    <size android:width="105sp" android:height="40sp"/>
</shape>

cancel_button_style.xml 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="1000sp"/>
    <solid android:color="@color/white"/>
    <stroke
        android:width="0.5sp"
        android:color="@color/teal_200"/>
    <size android:width="105sp" android:height="40sp"/>
</shape>

4. 自定義dialog的使用

final AlertDialog dialog = new AlertDialog.Builder(xxxClass.this).create();
dialog.setCancelable(false); //點擊對話框以外的位置,不消失
dialog.show();
 
Window window = dialog.getWindow();
window.setContentView(R.layout.dialog);
//標題
TextView title = window.findViewById(R.id.dialog_title);
title.setText("dialog_title");
 
//內容
TextView message = window.findViewById(R.id.dialog_message);
message.setText("dialog_message ");
 
//確定按鈕
LinearLayout confirm = window.findViewById(R.id.dialog_confirm);
confirm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //xxx
    }
});
 
//取消按鈕
LinearLayout cancel = window.findViewById(R.id.dialog_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //xxx
    }
});

感謝各位的閱讀!關于“android如何自定義對話框”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI