Android使用CardView实现圆角对话框

所属分类: 软件编程 / Android 阅读数: 52
收藏 0 赞 0 分享

前言:随着用户体验的不断的加深,良好的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设置背景色,会导致左上和右上不会变成圆角)。

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

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多