Android使用CardView實(shí)現(xiàn)圓角對(duì)話框
前言:隨著用戶(hù)體驗(yàn)的不斷的加深,良好的UI視覺(jué)效果也必不可少,以前方方正正的對(duì)話框樣式在APP已不復(fù)存在,取而代之的是帶有圓角效果的Dialog,之前設(shè)置對(duì)畫(huà)框的圓角效果都是通過(guò)drawable/shape屬性來(lái)完成,隨著Google API的不斷更新,API 21(Android 5.0)添加了新的控件CardView,這使得圓角的實(shí)現(xiàn)更加方便快捷。
效果圖:
導(dǎo)入CardView依賴(lài)(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="溫馨提示:確定修改維護(hù)詳情信息?" 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屬性:設(shè)置圓角的弧度大小,這里設(shè)置的為10dp
2.CardView還有padding、cardUseCompatPadding(內(nèi)邊距)、background等屬性
3.CardView繼承自FrameLayout,使用時(shí)可以重新嵌套布局
2.代碼實(shí)現(xiàn)
/** * 展示對(duì)話框 */ private void showDialog(String title) { //初始化布局文件 View dialogView = View.inflate(mContext, R.layout.dialog_layout_test, null); //標(biāo)題 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); //設(shè)置標(biāo)題及描述信息 tvTitle.setText(title); tvDes.setText("退出當(dāng)前登錄后將要重新登錄!"); //確定和取消按鈕監(jiān)聽(tīng)事件 tvConfirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(mContext,LoginActivity.class); startActivity(intent); UIUtil.toast("退出成功,請(qǐng)重新登錄"); 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(); //設(shè)置背景色為透明,解決設(shè)置圓角后有白色直角的問(wèn)題 Window window=mDialog.getWindow(); window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); mDialog.setView(dialogView); mDialog.setCanceledOnTouchOutside(false);//點(diǎn)擊屏幕不消失 mDialog.show(); //設(shè)置參數(shù)必須在show之后,不然沒(méi)有效果 WindowManager.LayoutParams params = mDialog.getWindow().getAttributes(); mDialog.getWindow().setAttributes(params); }
使用的是V7包的AlertDialog實(shí)現(xiàn)的,當(dāng)然也可以使用Dialog實(shí)現(xiàn)。
總結(jié):CardView實(shí)現(xiàn)對(duì)話框的圓角效果更加的方便,不用編寫(xiě)shape屬性,當(dāng)標(biāo)題欄需要背景色時(shí),也無(wú)需考慮設(shè)置標(biāo)題欄的shape(不使用CardView時(shí),如果不使用shape設(shè)置背景色,會(huì)導(dǎo)致左上和右上不會(huì)變成圓角)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android控件CardView實(shí)現(xiàn)卡片效果
- Android控件CardView實(shí)現(xiàn)卡片布局
- Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁(yè)動(dòng)畫(huà)的方法
- Android使用CardView作為RecyclerView的Item并實(shí)現(xiàn)拖拽和左滑刪除
- Android CardView詳解及使用方法和實(shí)例
- Android中使用CircleImageView和Cardview制作圓形頭像的方法
- Android應(yīng)用開(kāi)發(fā)中CardView的初步使用指南
- Android MaterialCardView的使用介紹與示例
相關(guān)文章
TabLayout+ViewPager2的簡(jiǎn)單使用詳解
這篇文章主要為大家詳細(xì)介紹了TabLayout+ViewPager2的簡(jiǎn)單使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android實(shí)現(xiàn)炫酷的CheckBox效果
大家是不是對(duì)系統(tǒng)自帶的CheckBox產(chǎn)生乏味感了呢?今天這篇文章給大家?guī)?lái)的是一款全新的CheckBox,下面來(lái)一起看看下面的CheckBox吧!有需要的朋友們可以參考借鑒。2016-10-10Android中自定義ContentProvider實(shí)例
應(yīng)用A(TestBaidu)調(diào)用另外一個(gè)應(yīng)用(TestContentProvider)即自定義ContentProvider的使用,其它應(yīng)用調(diào)用該ContentProvider,具體如下,感興趣的朋友可以參考下哈2013-06-06android基礎(chǔ)教程之a(chǎn)ndroid的listview與edittext沖突解決方法
這篇文章主要介紹了android的listview與edittext沖突解決方法,需要的朋友可以參考下2014-02-02Android 無(wú)障礙全局懸浮窗實(shí)現(xiàn)示例
本文主要介紹了Android 無(wú)障礙全局懸浮窗實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06android view實(shí)現(xiàn)橫向滑動(dòng)選擇
這篇文章主要為大家詳細(xì)介紹了android view實(shí)現(xiàn)橫向滑動(dòng)選擇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android開(kāi)發(fā)手冊(cè)TextInputLayout樣式使用示例
這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)TextInputLayout樣式使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Android基于AudioManager、PhoneStateListener實(shí)現(xiàn)設(shè)置黑名單功能
這篇文章主要介紹了Android基于AudioManager、PhoneStateListener實(shí)現(xiàn)設(shè)置黑名單功能的方法,涉及Android操作手機(jī)通信錄及通話模式與手機(jī)狀態(tài)的相關(guān)技巧,需要的朋友可以參考下2016-01-01Android對(duì)話框自定義標(biāo)題 對(duì)話框標(biāo)題美化操作
這篇文章主要為大家詳細(xì)介紹了Android對(duì)話框自定義標(biāo)題的相關(guān)資料,如何對(duì)對(duì)話框標(biāo)題進(jìn)行美化操作,感興趣的小伙伴們可以參考一下2016-08-08