Android中圆角对话框的实现(总结)

效果图如下:

其中最重要的一点就是:要将对话框的背景设置为透明的,其他的圆角之类的可以通过Shape等修改。

1、需要把对话框的默认背景改为透明。

方法:自定义Dialog的样式。

<!--对话框透明背景-->
<style name="Translucent_NoTitle" parent="android:style/Theme.Dialog">

    <!--边框-->
    <item name="android:windowFrame">@null</item>
    <!--是否浮现在activity之上-->
    <item name="android:windowIsFloating">true</item>
    <!--半透明-->
    <item name="android:windowIsTranslucent">false</item>
    <!--无标题-->
    <item name="android:windowNoTitle">true</item>
    <!--提示框背景(透明)-->
    <item name="android:windowBackground">@color/transparent</item>
    <!--模糊-->
    <item name="android:backgroundDimEnabled">true</item>

</style>
<color name="transparent">#00000000</color>

如何使用:

final Dialog dialog = new Dialog(mContext, R.style.Translucent_NoTitle);

2、布局文件XML

2.1 layout_cornerdialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提示内容"
        android:textSize="14dp"
        android:gravity="center_horizontal"
        android:background="@drawable/bg_corner_top"
        android:padding="10dp"
        android:textStyle="bold"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorLightGray"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="你确定要退出当前系统吗?"
        android:padding="10dp"
        android:background="@color/colorWhite"
        android:textSize="14dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Layout_CornerDialog_btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_corner_leftbottom"
            android:textSize="14dp"
            android:text="取消"/>

        <Button
            android:id="@+id/Layout_CornerDialog_btnBesure"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_corner_rightbottom"
            android:textSize="14dp"
            android:text="确定"/>

    </LinearLayout>

</LinearLayout>

2.2、layout_cornerdialog_withclose.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提示内容"
            android:textSize="14dp"
            android:gravity="center_horizontal"
            android:background="@drawable/bg_corner_top"
            android:padding="10dp"
            android:textStyle="bold"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorLightGray"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="你确定要退出当前系统吗?"
            android:padding="10dp"
            android:background="@color/colorWhite"
            android:textSize="14dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/Layout_CornerDialog_btnCancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/btn_corner_leftbottom"
                android:textSize="14dp"
                android:text="取消"/>

            <Button
                android:id="@+id/Layout_CornerDialog_btnBesure"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/btn_corner_rightbottom"
                android:textSize="14dp"
                android:text="确定"/>

        </LinearLayout>

    </LinearLayout>

    <ImageView
        android:id="@+id/Layout_CornerDialog_ivCancel"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:scaleType="fitXY"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:src="@mipmap/icon_close"/>

</RelativeLayout>

2.3、btn_corner_leftbottom.xml,其他相关的样式文件类似

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorWhite"/>
    <stroke android:color="@color/colorLightGray" android:width="1dp"/>
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="0dp"
        android:topRightRadius="0dp"
        android:topLeftRadius="0dp"/>
</shape>

3、如何在Activity中引用

/**
 * 显示圆角对话框
 */
private void showCornerDialog() {
    final Dialog dialog = new Dialog(mContext, R.style.Translucent_NoTitle);
    View view = LayoutInflater.from(mContext).inflate(R.layout.layout_cornerdialog, null);
    Button btnCancel = view.findViewById(R.id.Layout_CornerDialog_btnCancel);
    Button btnBesure = view.findViewById(R.id.Layout_CornerDialog_btnBesure);
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });
    btnBesure.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });
    dialog.setContentView(view);
    dialog.show();
}

/**
 * 显示圆角对话框(带有悬浮关闭按钮)
 */
private void showCornerDialogWithClose() {
    final Dialog dialog = new Dialog(mContext, R.style.Translucent_NoTitle);
    View view = LayoutInflater.from(mContext).inflate(R.layout.layout_cornerdialog_withclose, null);
    Button btnCancel = view.findViewById(R.id.Layout_CornerDialog_btnCancel);
    Button btnBesure = view.findViewById(R.id.Layout_CornerDialog_btnBesure);
    ImageView ivClose = view.findViewById(R.id.Layout_CornerDialog_ivCancel);
    ivClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });
    btnBesure.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });
    dialog.setContentView(view);
    dialog.show();
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值