电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Android修改Dialog樣式的方法

瀏覽:45日期:2022-09-18 14:43:56
目錄一、Dialog源碼解析1.1 new AlertDialog.Builder(this).create()1.2 AlertController二、修改Dialog樣式2.1 通過findViewById2.2 自定義style一、Dialog源碼解析1.1 new AlertDialog.Builder(this).create()

protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {super(context, resolveDialogTheme(context, themeResId));//創(chuàng)建AlertController,是Dialog布局相關(guān)代碼mAlert = new AlertController(getContext(), this, getWindow()); }@NonNullpublic AlertDialog create() { // We can’t use Dialog’s 3-arg constructor with the createThemeContextWrapper param, // so we always have to re-set the theme final AlertDialog dialog = new AlertDialog(P.mContext, mTheme); P.apply(dialog.mAlert); dialog.setCancelable(P.mCancelable); if (P.mCancelable) {dialog.setCanceledOnTouchOutside(true); } dialog.setOnCancelListener(P.mOnCancelListener); dialog.setOnDismissListener(P.mOnDismissListener); if (P.mOnKeyListener != null) {dialog.setOnKeyListener(P.mOnKeyListener); } return dialog;}public void apply(AlertController dialog) { if (mCustomTitleView != null) {dialog.setCustomTitle(mCustomTitleView); } else {if (mTitle != null) { dialog.setTitle(mTitle);}if (mIcon != null) { dialog.setIcon(mIcon);}if (mIconId != 0) { dialog.setIcon(mIconId);} .......... AlertDialog 構(gòu)造函數(shù)中會創(chuàng)建 AlertController,用來控制對話框的布局 P.apply(dialog.mAlert); 將用戶自定義的配置賦值給 AlertController 1.2 AlertController

public AlertController(Context context, AppCompatDialog di, Window window) {mContext = context;mDialog = di;mWindow = window;mHandler = new ButtonHandler(di);final TypedArray a = context.obtainStyledAttributes(null, R.styleable.AlertDialog,R.attr.alertDialogStyle, 0);mAlertDialogLayout = a.getResourceId(R.styleable.AlertDialog_android_layout, 0);mButtonPanelSideLayout = a.getResourceId(R.styleable.AlertDialog_buttonPanelSideLayout, 0);mListLayout = a.getResourceId(R.styleable.AlertDialog_listLayout, 0);mMultiChoiceItemLayout = a.getResourceId(R.styleable.AlertDialog_multiChoiceItemLayout, 0);mSingleChoiceItemLayout = a.getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0);mListItemLayout = a.getResourceId(R.styleable.AlertDialog_listItemLayout, 0);mShowTitle = a.getBoolean(R.styleable.AlertDialog_showTitle, true);mButtonIconDimen = a.getDimensionPixelSize(R.styleable.AlertDialog_buttonIconDimen, 0);a.recycle();/* We use a custom title so never request a window title */di.supportRequestWindowFeature(Window.FEATURE_NO_TITLE); }

R.attr.alertDialogStyle 是 對話框的默認(rèn)樣式,

<item name='alertDialogStyle'>@style/AlertDialog.AppCompat</item><style name='AlertDialog.AppCompat' parent='Base.AlertDialog.AppCompat'/> <style name='Base.AlertDialog.AppCompat' parent='android:Widget'><item name='android:layout'>@layout/abc_alert_dialog_material</item><item name='listLayout'>@layout/abc_select_dialog_material</item><item name='listItemLayout'>@layout/select_dialog_item_material</item><item name='multiChoiceItemLayout'>@layout/select_dialog_multichoice_material</item><item name='singleChoiceItemLayout'>@layout/select_dialog_singlechoice_material</item><item name='buttonIconDimen'>@dimen/abc_alert_dialog_button_dimen</item> </style>

上述代碼可以看出,abc_alert_dialog_material 就是dialog的默認(rèn)布局。

<androidx.appcompat.widget.AlertDialogLayout xmlns:android='http://schemas.android.com/apk/res/android' android: android:layout_width='match_parent' android:layout_height='wrap_content' android:gravity='start|left|top' android:orientation='vertical'> <include layout='@layout/abc_alert_dialog_title_material'/> <FrameLayoutandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:minHeight='48dp'><View android: android:layout_width='match_parent' android:layout_height='1dp' android:layout_gravity='top' android:background='?attr/colorControlHighlight' android:visibility='gone'/><androidx.core.widget.NestedScrollView android: android:layout_width='match_parent' android:layout_height='wrap_content' android:clipToPadding='false'> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='vertical'><android.widget.Space android: android:layout_width='match_parent' android:layout_height='@dimen/abc_dialog_padding_top_material' android:visibility='gone'/><TextView android: android:layout_width='match_parent' android:layout_height='wrap_content' android:paddingLeft='?attr/dialogPreferredPadding' android:paddingRight='?attr/dialogPreferredPadding'/><android.widget.Space android: android:layout_width='match_parent' android:layout_height='@dimen/abc_dialog_padding_top_material' android:visibility='gone'/> </LinearLayout></androidx.core.widget.NestedScrollView><View android: android:layout_width='match_parent' android:layout_height='1dp' android:layout_gravity='bottom' android:background='?attr/colorControlHighlight' android:visibility='gone'/> </FrameLayout> <FrameLayoutandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:minHeight='48dp'><FrameLayout android: android:layout_width='match_parent' android:layout_height='wrap_content'/> </FrameLayout> <include layout='@layout/abc_alert_dialog_button_bar_material' android:layout_width='match_parent' android:layout_height='wrap_content'/></androidx.appcompat.widget.AlertDialogLayout>

標(biāo)題布局:

<!-- abc_alert_dialog_title_material: --><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android: android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='vertical'> <!-- If the client uses a customTitle, it will be added here. --> <LinearLayoutandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:gravity='center_vertical|start|left'android:orientation='horizontal'android:paddingLeft='?attr/dialogPreferredPadding'android:paddingRight='?attr/dialogPreferredPadding'android:paddingTop='@dimen/abc_dialog_padding_top_material'><ImageView android: android:layout_width='32dip' android:layout_height='32dip' android:layout_marginEnd='8dip' android:layout_marginRight='8dip' android:scaleType='fitCenter' android:src='http://www.hdgsjgj.cn/bcjs/@null'/><androidx.appcompat.widget.DialogTitle android: android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_gravity='start' android:ellipsize='end' android:singleLine='true' android:textAlignment='viewStart'/> </LinearLayout> <android.widget.Spaceandroid: android:layout_width='match_parent'android:layout_height='@dimen/abc_dialog_title_divider_material'android:visibility='gone'/></LinearLayout>

按鈕布局:

<!-- abc_alert_dialog_button_bar_material: --><ScrollView xmlns:android='http://schemas.android.com/apk/res/android' android: android:layout_width='match_parent' android:layout_height='wrap_content' android:fillViewport='true' android:scrollIndicators='top|bottom'> <androidx.appcompat.widget.ButtonBarLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:gravity='bottom'android:layoutDirection='locale'android:orientation='horizontal'android:paddingBottom='4dp'android:paddingLeft='12dp'android:paddingRight='12dp'android:paddingTop='4dp'><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content'/><android.widget.Space android: android:layout_width='0dp' android:layout_height='0dp' android:layout_weight='1' android:visibility='invisible'/><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content'/><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content'/> </androidx.appcompat.widget.ButtonBarLayout></ScrollView>二、修改Dialog樣式2.1 通過findViewById

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(msg); builder.setPositiveButton(getString(R.string.yes), null); AlertDialog dialog = builder.create(); dialog.show(); //直接通過id找到對應(yīng)的控件 Button button = dialog.findViewById(android.R.id.button1); //或者通過getButton方法也可以獲取到 Button button2 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

這種修改方式必須在 show() 之后調(diào)用,否則會出現(xiàn)空指針異常。這個(gè)是因?yàn)椋瑘?zhí)行 show() 方法的時(shí)候,dialog才會初始化布局,具體源碼可以查看 Dialog 的 onCreate 方法。

2.2 自定義style

通過上面源碼可以發(fā)現(xiàn),Dialog三個(gè)按鈕的樣式如下:

buttonBarNeutralButtonStyle buttonBarNegativeButtonStyle buttonBarPositiveButtonStyle

<Button android: android:layout_width='wrap_content' android:layout_height='wrap_content'/><android.widget.Space android: android:layout_width='0dp' android:layout_height='0dp' android:layout_weight='1' android:visibility='invisible'/><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content'/><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content'/>

自定義樣式替換上述 style即可達(dá)到修改效果。

在style.xml添加如下代碼:

<style name='accessPositiveBtnStyle' parent='Widget.AppCompat.Button.ButtonBar.AlertDialog'><item name='android:textColor'>@color/test1</item> </style> <style name='accessNegativeBtnStyle' parent='Widget.AppCompat.Button.ButtonBar.AlertDialog'><item name='android:textColor'>@color/test2</item> </style> <!-- 彈出框樣式 --> <style name='testDialogTheme' parent='Theme.AppCompat.Light.Dialog.Alert'><item name='buttonBarPositiveButtonStyle'>@style/accessPositiveBtnStyle</item><item name='buttonBarNegativeButtonStyle'>@style/accessNegativeBtnStyle</item> </style>

具體使用:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.testDialogTheme); builder.setMessage('Test'); builder.setCancelable(false); builder.setPositiveButton('確認(rèn)', null); builder.setNegativeButton('取消', null); Dialog dialog = builder.create(); dialog.show();

以上就是Android修改Dialog樣式的方法的詳細(xì)內(nèi)容,更多關(guān)于Android修改Dialog樣式的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 深圳APP开发_手机软件APP定制外包_小程序开发公司-来科信 | 打造全球沸石生态圈 - 国投盛世| 罗氏牛血清白蛋白,罗氏己糖激酶-上海嵘崴达实业有限公司 | 震动筛选机|震动分筛机|筛粉机|振筛机|振荡筛-振动筛分设备专业生产厂家高服机械 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 瓶盖扭矩测试仪-瓶盖扭力仪-全自动扭矩仪-济南三泉中石单品站 | 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 存包柜厂家_电子存包柜_超市存包柜_超市电子存包柜_自动存包柜-洛阳中星 | 标准件-非标紧固件-不锈钢螺栓-非标不锈钢螺丝-非标螺母厂家-三角牙锁紧自攻-南京宝宇标准件有限公司 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 帽子厂家_帽子工厂_帽子定做_义乌帽厂_帽厂_制帽厂_帽子厂_浙江高普制帽厂 | 纸塑分离机-纸塑分离清洗机设备-压力筛-碎浆机厂家金双联环保 | 深圳公司注册-工商注册公司-千百顺代理记账公司 | 空气净化器租赁,空气净化器出租,全国直租_奥司汀净化器租赁 | 电子海图系统-电梯检验系统-智慧供热系统开发-商品房预售资金监管系统 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | 电磁辐射仪-电磁辐射检测仪-pm2.5检测仪-多功能射线检测仪-上海何亦仪器仪表有限公司 | 食品级焦亚硫酸钠_工业级焦亚硫酸钠_焦亚硫酸钠-潍坊邦华化工有限公司 | 粉末包装机,拆包机厂家,价格-上海强牛包装机械设备有限公司 | 大连海岛旅游网>>大连旅游,大连海岛游,旅游景点攻略,海岛旅游官网 | 上海律师事务所_上海刑事律师免费咨询平台-煊宏律师事务所 | 焊缝跟踪系统_激光位移传感器_激光焊缝跟踪传感器-创想智控 | 机制砂选粉机_砂石选粉机厂家-盐城市助成粉磨科技有限公司 | 液氮罐(生物液氮罐)百科-无锡爱思科 | 巩义市科瑞仪器有限公司| 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 广州食堂承包_广州团餐配送_广州堂食餐饮服务公司 - 旺记餐饮 | 新中天检测有限公司青岛分公司-山东|菏泽|济南|潍坊|泰安防雷检测验收 | 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | DWS物流设备_扫码称重量方一体机_快递包裹分拣机_广东高臻智能装备有限公司 | 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 上海公众号开发-公众号代运营公司-做公众号的公司企业服务商-咏熠软件 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 彩超机-黑白B超机-便携兽用B超机-多普勒彩超机价格「大为彩超」厂家 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 |