a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android使用CardView實現圓角對話框

2019-10-21 21:34:59
字體:
來源:轉載
供稿:網友

前言:隨著用戶體驗的不斷的加深,良好的UI視覺效果也必不可少,以前方方正正的對話框樣式在APP已不復存在,取而代之的是帶有圓角效果的Dialog,之前設置對畫框的圓角效果都是通過drawable/shape屬性來完成,隨著Google API的不斷更新,API 21(Android 5.0)添加了新的控件CardView,這使得圓角的實現更加方便快捷。

效果圖:

Android,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設置背景色,會導致左上和右上不會變成圓角)。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中文字幕第七页 | 亚洲国产aⅴ成人精品无吗 91精品国产一区二区 | 欧美中文字幕在线观看 | 日韩3级| 久久成人免费视频 | av成人在线观看 | 国产精品一品二区三区的使用体验 | 久久一区 | 国产一区二区三区免费 | 欧美久久久久久久久久久久 | 国产欧美久久一区二区三区 | 一本a道v久大 | 国产欧美精品一区二区色综合 | 久久久久99 | 日韩一区二区视频 | 在线中文字幕日韩 | 欧美精品国产精品 | 国产成人久久 | 亚洲天堂av中文字幕 | 国产精品一区二区三区在线 | 99动漫 | 成人高清视频在线观看 | 天堂精品一区 | 欧美成人精品一区二区 | 国产精品丝袜一区二区 | 色爱区综合 | 成人亚洲精品久久久久 | www.国产.com| 日韩大片在线观看 | 久久久久久久久久久久久国产精品 | 国产精品不卡视频 | 免费毛片网 | 久久叉| 欧美一区二区视频 | 天天干夜夜爽 | 国产精品视屏 | 欧美一卡二卡 | 一区二区三区在线视频免费观看 | 欧美黄视频在线观看 | 午夜精品久久久久久久久久久久 | 国产一区二区三区久久久久久久久 |