在线直播源码,自定义AlertDialog设置宽高并去掉默认的边框
在线直播源码,自定义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="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/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设置宽高并去掉默认的边框, 更多内容欢迎关注之后的文章