欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Dialog设置圆角无效

程序员文章站 2022-03-28 16:14:36
自己写了一个通用的Dialog,但是在drawable中设置的圆角一直不起作用。后来找到一篇博客,解决了问题 https://blog.csdn.net/denglusha737/article/details/63255577public class CommonDialog extends Dialog{ private TextView tvTitle; private TextView tvContent; private TextView tvSure; pr....

自己写了一个通用的Dialog,但是在drawable中设置的圆角一直不起作用。后来找到一篇博客,解决了问题 https://blog.csdn.net/denglusha737/article/details/63255577

public class CommonDialog extends Dialog{
    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvSure;
    private TextView tvCancel;

    public CommonDialog(@NonNull Context context, String title, String content, String sure, String cancel) {
        super(context);
        setContentView(R.layout.dialog_common);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));  //圆角起作用
        initView();
        conver(title, content, sure, cancel);
    }
    public void initView(){
        tvTitle = findViewById(R.id.dialog_title);
        tvContent = findViewById(R.id.dialog_content);
        tvSure = findViewById(R.id.dialog_sure);
        tvCancel = findViewById(R.id.dialog_cancel);
    }
    public void conver(String title, String content, String sure, String cancel){
        tvTitle.setText(title);
        tvContent.setText(content);
        tvSure.setText(sure);
        tvCancel.setText(cancel);
    }

    public void setItemClickLisenter(final OnItemClickListener listener) {
          tvSure.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  listener.onItemClickListener(tvSure);
              }
          });
          tvCancel.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  listener.onItemClickListener(tvCancel);
              }
          });
    }
    public interface OnItemClickListener{
        public void onItemClickListener(TextView textView);
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="250dp"
    android:layout_height="188dp"
    android:background="@drawable/bg_dialog">
    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="7dp"
        android:textColor="#000000"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="13dp"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginTop="10dp" />
    <TextView
        android:id="@+id/dialog_content"
        android:layout_width="match_parent"
        android:layout_height="67dp"
        android:textColor="#000000"
        android:textSize="3dp"
        android:layout_marginTop="18.5dp"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"/>
    <View
        android:layout_width="268dp"
        android:layout_height="1dp"
        android:background="#000000"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/dialog_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#000000"
            android:textSize="16sp"
            android:gravity="center"/>
        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="#000000"/>
        <TextView
            android:id="@+id/dialog_sure"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#000000"
            android:textSize="16sp"
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#fff"/>
    <corners android:radius="@dimen/dimen20dp"/>
</shape>

在代码中调用:

CommonDialog commonDialog = new CommonDialog(getContext(), getString(R.string.title_virtuallock),getString(R.string.content_virtuallock),getString(R.string.sure_virtuallock),getString(R.string.cancel_virtuallock));
commonDialog.setItemClickLisenter(new CommonDialog.OnItemClickListener() {
	@Override
    public void onItemClickListener(TextView textView) {
    	if(textView.getId() == R.id.dialog_sure){
   			setPwdIntent = new Intent(getActivity(), SetPwdActivity.class);
        	startActivity(setPwdIntent);
       		virtuallockDialog.dismiss();
        }else if(textView.getId() == R.id.dialog_cancel){
            virtuallockDialog.dismiss();
       }
  }
});
virtuallockDialog.show();

为什么圆角会不起作用呢?

google自己设置了默认的背景,虽然我们设置了view为圆角,但是在挂在已有背景下,我们就是自然看不到,看下源码,google也有说道透明问题。

本文地址:https://blog.csdn.net/aaqian1/article/details/107577076