Android使用CardView实现圆角对话框

 更新时间:2018年11月28日 17:26:38   作者:ruancw  
这篇文章主要为大家详细介绍了Android使用CardView实现圆角对话框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言:随着用户体验的不断的加深,良好的UI视觉效果也必不可少,以前方方正正的对话框样式在APP已不复存在,取而代之的是带有圆角效果的Dialog,之前设置对画框的圆角效果都是通过drawable/shape属性来完成,随着Google API的不断更新,API 21(Android 5.0)添加了新的控件CardView,这使得圆角的实现更加方便快捷。

效果图:

导入CardView依赖(API 21新控件)

implementation 'com.android.support:cardview-v7:26.1.0'

1.布局引用

<android.support.v7.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:cardCornerRadius="@dimen/dp_10">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
      android:id="@+id/tv_title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/colorTabClick"
      android:gravity="center"
      android:padding="@dimen/dp_10"
      android:text="温馨提示:确定修改维护详情信息?"
      android:textColor="@color/bg_mainWhite"
      android:textSize="@dimen/dp_16" />

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

    <TextView
      android:id="@+id/tv_des"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="@dimen/dp_10"
      android:gravity="top"
      />

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

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

      <TextView
        android:id="@+id/tv_cancel"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="取消"
        android:textSize="@dimen/dp_16" />

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

      <TextView
        android:id="@+id/tv_confirm"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="确定"
        android:textSize="@dimen/dp_16" />
    </LinearLayout>
  </LinearLayout>

</android.support.v7.widget.CardView>

1.cardCornerRadius属性:设置圆角的弧度大小,这里设置的为10dp

2.CardView还有padding、cardUseCompatPadding(内边距)、background等属性

3.CardView继承自FrameLayout,使用时可以重新嵌套布局

2.代码实现

/**
 * 展示对话框
 */
private void showDialog(String title) {
  //初始化布局文件
  View dialogView = View.inflate(mContext, R.layout.dialog_layout_test, null);
  //标题
  TextView tvTitle = (TextView) dialogView.findViewById(R.id.tv_title);
  //确定按钮
  TextView tvConfirm = (TextView) dialogView.findViewById(R.id.tv_confirm);
  //取消按钮
  TextView tvCancel = (TextView) dialogView.findViewById(R.id.tv_cancel);
  //描述信息
  TextView tvDes= (TextView) dialogView.findViewById(R.id.tv_des);
  //设置标题及描述信息
  tvTitle.setText(title);
  tvDes.setText("退出当前登录后将要重新登录!");
  //确定和取消按钮监听事件
  tvConfirm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      Intent intent = new Intent(mContext,LoginActivity.class);
      startActivity(intent);
      UIUtil.toast("退出成功,请重新登录");
      getActivity().finish();
      mDialog.dismiss();
    }
  });
  tvCancel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      mDialog.dismiss();
    }
  });
  mMessageBuilder = new AlertDialog.Builder(mContext);
  mDialog = mMessageBuilder.create();
  //设置背景色为透明,解决设置圆角后有白色直角的问题
  Window window=mDialog.getWindow();
  window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  mDialog.setView(dialogView);
  mDialog.setCanceledOnTouchOutside(false);//点击屏幕不消失
  mDialog.show();
  //设置参数必须在show之后,不然没有效果
  WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
  mDialog.getWindow().setAttributes(params);
}

使用的是V7包的AlertDialog实现的,当然也可以使用Dialog实现。

总结:CardView实现对话框的圆角效果更加的方便,不用编写shape属性,当标题栏需要背景色时,也无需考虑设置标题栏的shape(不使用CardView时,如果不使用shape设置背景色,会导致左上和右上不会变成圆角)。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • TabLayout+ViewPager2的简单使用详解

    TabLayout+ViewPager2的简单使用详解

    这篇文章主要为大家详细介绍了TabLayout+ViewPager2的简单使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • gradle中的properties文件详解

    gradle中的properties文件详解

    这篇文章主要介绍了gradle中的properties文件详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Android实现炫酷的CheckBox效果

    Android实现炫酷的CheckBox效果

    大家是不是对系统自带的CheckBox产生乏味感了呢?今天这篇文章给大家带来的是一款全新的CheckBox,下面来一起看看下面的CheckBox吧!有需要的朋友们可以参考借鉴。
    2016-10-10
  • Android中自定义ContentProvider实例

    Android中自定义ContentProvider实例

    应用A(TestBaidu)调用另外一个应用(TestContentProvider)即自定义ContentProvider的使用,其它应用调用该ContentProvider,具体如下,感兴趣的朋友可以参考下哈
    2013-06-06
  • android基础教程之android的listview与edittext冲突解决方法

    android基础教程之android的listview与edittext冲突解决方法

    这篇文章主要介绍了android的listview与edittext冲突解决方法,需要的朋友可以参考下
    2014-02-02
  • Android 无障碍全局悬浮窗实现示例

    Android 无障碍全局悬浮窗实现示例

    本文主要介绍了Android 无障碍全局悬浮窗实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • android view实现横向滑动选择

    android view实现横向滑动选择

    这篇文章主要为大家详细介绍了android view实现横向滑动选择,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android开发手册TextInputLayout样式使用示例

    Android开发手册TextInputLayout样式使用示例

    这篇文章主要为大家介绍了Android开发手册TextInputLayout样式使用示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Android基于AudioManager、PhoneStateListener实现设置黑名单功能

    Android基于AudioManager、PhoneStateListener实现设置黑名单功能

    这篇文章主要介绍了Android基于AudioManager、PhoneStateListener实现设置黑名单功能的方法,涉及Android操作手机通信录及通话模式与手机状态的相关技巧,需要的朋友可以参考下
    2016-01-01
  • Android对话框自定义标题 对话框标题美化操作

    Android对话框自定义标题 对话框标题美化操作

    这篇文章主要为大家详细介绍了Android对话框自定义标题的相关资料,如何对对话框标题进行美化操作,感兴趣的小伙伴们可以参考一下
    2016-08-08

最新评论