当前位置: 移动技术网 > IT编程>移动开发>Android > Android使用CardView实现圆角对话框

Android使用CardView实现圆角对话框

2019年07月31日  | 移动技术网IT编程  | 我要评论

力证逍遥,都市搜客团,范冰冰苹果快播

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网