您的当前位置:首页正文

Android实现圆角弹框功能

2024-11-17 来源:个人技术集锦

自定义弹窗类——Android 透明圆角弹窗

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

public class MySetDailog extends AlertDialog {
  TextView tvmydailog01;
  TextView tvmydailog02;
  ImageView ivdailog;
  public MySetDailog(@NonNull Context context ) {
    super(context);
    setCanceledOnTouchOutside(true);//点击其他区域时  true 关闭弹窗 false 不关闭弹窗
    tvmydailog01 = findViewById(R.id.tv_mydailog01);
    tvmydailog02 = findViewById(R.id.tv_mydailog02);
    ivdailog = findViewById(R.id.iv_mydailog);
  }

  /*设置图片接口*/
  public void setImageResource(int resId){
    ivdailog.setImageResource(resId);
  }

  /*设置文字接口*/
  public void setText01(String str){
    tvmydailog01.setText(str);
  }
  public void setText02(String str){
    tvmydailog02.setText(str);
  }
  /*设置文字颜色接口*/
  public void setColor01(int color01){
    tvmydailog01.setTextColor(color01);
  }
  public void setColor02(int color02){
    tvmydailog02.setTextColor(color02);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_mydailog);//加载布局
    //设置透明背景
    Window window = getWindow();
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

  }
}

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/dialog_view"
  android:layout_width="325dp"
  android:layout_height="162dp"
  android:paddingTop="15dp"
  android:orientation="vertical"
  android:layout_gravity="center_horizontal"
  android:gravity="center"
  android:background="@drawable/bg_round_dailog">



    <ImageView
      android:id="@+id/iv_mydailog"
      android:layout_width="wrap_content"
      android:layout_height="66dp"
      android:src="@drawable/icon_warning" />

    <TextView
      android:id="@+id/tv_mydailog01"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="@dimen/dp_12"
      android:maxLines="1"
      android:text="@string/str_upload_fail"
      android:textColor="#BB340F"
      android:textStyle="bold"
      android:textSize="18sp" />

    <TextView
      android:id="@+id/tv_mydailog02"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/str_push_true_phone"
      android:textColor="#ff555555"
      android:textSize="15sp"
      />

</LinearLayout>

调用

 MySetDailog mySetDailog = new MySetDailog(BindActivity.this);
        mySetDailog.show();
显示全文