線上直播原始碼,自定義AlertDialog設定寬高並去掉預設的邊框

zhibo系統開發發表於2023-04-26

線上直播原始碼,自定義AlertDialog設定寬高並去掉預設的邊框

1、先寫一個自定義的AlertDialog。

package com.phone.common_library.dialog;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
 
import com.phone.common_library.R;
import com.phone.common_library.callback.OnCommonSingleParamCallback;
import com.phone.common_library.callback.OnItemViewClickListener;
 
public class StandardDialog {
 
    private AlertDialog alertDialog;
 
    private TextView tevTitle;
    private TextView tevContent;
    private View viewHorizontalLine;
    private TextView tevCancel;
    private View viewVerticalLine;
    private TextView tevOk;
 
 
    @SuppressLint("RestrictedApi")
    public StandardDialog(@NonNull Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_standard, null, false);
        tevTitle = (TextView) view.findViewById(R.id.tev_title);
        tevContent = (TextView) view.findViewById(R.id.tev_content);
        viewHorizontalLine = (View) view.findViewById(R.id.view_horizontal_line);
        tevCancel = (TextView) view.findViewById(R.id.tev_cancel);
        viewVerticalLine = (View) view.findViewById(R.id.view_vertical_line);
        tevOk = (TextView) view.findViewById(R.id.tev_ok);
 
 
        //設定R.style.dialog_decimal_style和setView(view, 0, 0, 0, 0)就可以去掉
        //AlertDialog的預設邊框,此時AlertDialog的layout的寬高就是AlertDialog的寬高
        alertDialog = new AlertDialog.Builder(context, R.style.standard_dialog_style)
                .setView(view)
                .create();
        tevCancel.setOnClickListener(v -> {
            onItemViewClickListener.onItemClickListener(0, v);
        });
        tevOk.setOnClickListener(v -> {
            onItemViewClickListener.onItemClickListener(1, v);
        });
        alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                onCommonSingleParamCallback.onSuccess("");
            }
        });
        alertDialog.show();
 
        Window window = alertDialog.getWindow();
        if (window != null) {
//            window.setBackgroundDrawableResource(android.R.color.transparent);
            // 取消這些邊框的關鍵程式碼
            window.setBackgroundDrawable(null);
            window.setGravity(Gravity.CENTER);
//            window.setWindowAnimations(R.style.PictureThemeDialogWindowStyle);
            WindowManager.LayoutParams params = window.getAttributes();
            window.setAttributes(params);
            //把 DecorView 的預設 padding 取消,同時 DecorView 的預設大小也會取消
            window.getDecorView().setPadding(0, 0, 0, 0);
        }
    }
 
    public void setTevContent(String content) {
        tevContent.setText(content);
    }
 
    public void setTevCancelHide() {
        viewVerticalLine.setVisibility(View.GONE);
        tevCancel.setVisibility(View.GONE);
    }
 
    public void setCannotHide() {
        alertDialog.setCancelable(false);
        alertDialog.setCanceledOnTouchOutside(false);
    }
 
    public void hideStandardDialog() {
        if (alertDialog != null) {
            alertDialog.dismiss();
            alertDialog = null;
        }
    }
 
    private OnItemViewClickListener onItemViewClickListener;
 
    public void setOnItemViewClickListener(OnItemViewClickListener onItemViewClickListener) {
        this.onItemViewClickListener = onItemViewClickListener;
    }
 
    private OnCommonSingleParamCallback<String> onCommonSingleParamCallback;
 
    public void setOnCommonSingleParamCallback(OnCommonSingleParamCallback<String> onCommonSingleParamCallback) {
        this.onCommonSingleParamCallback = onCommonSingleParamCallback;
    }
 
}


2、AlertDialog的layout。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
    xmlns:tools="
    android:layout_width="@dimen/dp_300"
    android:layout_height="@dimen/dp_240"
    android:background="@drawable/corners_14_color_white"
    android:orientation="vertical">
 
    <!-- 注意:這裡一定要寫一個佈局,這個佈局和本佈局的根佈局寬高相同,要寫固定寬高,不要使用match_parent,不然寬高可能會失效 -->
    <LinearLayout
        android:layout_width="@dimen/dp_300"
        android:layout_height="@dimen/dp_240"
        android:orientation="vertical"
        tools:ignore="UselessParent">
 
        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_20" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_40"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:paddingStart="@dimen/dp_10"
            android:paddingEnd="@dimen/dp_10"
            android:text="Decimal"
            android:textColor="@color/blue"
            android:textSize="@dimen/sp_16" />
 
        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_20" />
 
        <EditText
            android:id="@+id/edt_input"
            android:layout_width="@dimen/dp_240"
            android:layout_height="@dimen/dp_40"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/corners_14_color_white_stroke_1_color_80000000"
            android:gravity="center_vertical"
            android:paddingStart="@dimen/dp_15"
            android:paddingEnd="@dimen/dp_15"
            android:textColor="@color/colorBlack333"
            android:textSize="@dimen/sp_16" />
 
        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_20" />
 
        <FrameLayout
            android:layout_width="@dimen/dp_200"
            android:layout_height="@dimen/dp_40"
            android:layout_gravity="center_horizontal">
 
            <TextView
                android:id="@+id/tev_cancel"
                android:layout_width="@dimen/dp_80"
                android:layout_height="@dimen/dp_40"
                android:layout_gravity="start"
                android:background="@drawable/corners_14_color_white_stroke_1_color_80000000"
                android:gravity="center"
                android:paddingStart="@dimen/dp_10"
                android:paddingEnd="@dimen/dp_10"
                android:text="cancel"
                android:textColor="@color/color_80000000"
                android:textSize="@dimen/sp_16" />
 
            <TextView
                android:id="@+id/tev_confirm"
                android:layout_width="@dimen/dp_80"
                android:layout_height="@dimen/dp_40"
                android:layout_gravity="end"
                android:background="@drawable/corners_14_color_white_stroke_1_color_blue"
                android:gravity="center"
                android:paddingStart="@dimen/dp_10"
                android:paddingEnd="@dimen/dp_10"
                android:text="confirm"
                android:textColor="@color/blue"
                android:textSize="@dimen/sp_16" />
 
        </FrameLayout>
    </LinearLayout>
 
</LinearLayout>


以上就是線上直播原始碼,自定義AlertDialog設定寬高並去掉預設的邊框, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2948945/,如需轉載,請註明出處,否則將追究法律責任。

相關文章