日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Android修改Dialog樣式的方法

瀏覽:113日期: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));//創建AlertController,是Dialog布局相關代碼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 構造函數中會創建 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 是 對話框的默認樣式,

<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的默認布局。

<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>

標題布局:

<!-- 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://m.b3g6.com/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找到對應的控件 Button button = dialog.findViewById(android.R.id.button1); //或者通過getButton方法也可以獲取到 Button button2 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

這種修改方式必須在 show() 之后調用,否則會出現空指針異常。這個是因為,執行 show() 方法的時候,dialog才會初始化布局,具體源碼可以查看 Dialog 的 onCreate 方法。

2.2 自定義style

通過上面源碼可以發現,Dialog三個按鈕的樣式如下:

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即可達到修改效果。

在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('確認', null); builder.setNegativeButton('取消', null); Dialog dialog = builder.create(); dialog.show();

以上就是Android修改Dialog樣式的方法的詳細內容,更多關于Android修改Dialog樣式的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲午夜国产成人| 91欧美极品| 国产一区二区三区四区五区| 另类综合日韩欧美亚洲| 精品国产aⅴ| 天堂中文在线播放| 国户精品久久久久久久久久久不卡| 在线观看免费一区二区| 亚洲资源网站| 国产伊人久久| 欧美日韩国产一区二区三区不卡| 视频一区中文字幕| 国产乱人伦丫前精品视频| sm捆绑调教国产免费网站在线观看| 私拍精品福利视频在线一区| 久久不射网站| 免费在线亚洲欧美| 亚洲四虎影院| 免费在线观看精品| 国际精品欧美精品| 狠狠爱成人网| 国产精品极品在线观看| 99久久精品网站| 日韩欧美中文字幕电影| 国产精成人品2018| 免费观看久久av| 久久国产日韩欧美精品| 欧洲一级精品| 日韩激情一二三区| 免费污视频在线一区| 亚洲精品在线a| 98精品视频| 蜜桃视频第一区免费观看| 精品高清久久| 蜜桃av一区二区三区电影| 精品久久国产一区| 亚洲网址在线观看| 欧洲av不卡| 91精品国产自产观看在线| 日韩另类视频| 国产精品久久久久久久久久久久久久久| 国户精品久久久久久久久久久不卡 | 捆绑调教美女网站视频一区| 99久久九九| 国产探花一区在线观看| 婷婷丁香综合| 麻豆久久一区| 中文字幕日韩亚洲| 久久精品影视| 欧美精品中文| 在线亚洲自拍| 久久久男人天堂| 日韩av网站在线免费观看| 成人日韩在线| 国产精品探花在线观看| 丝袜国产日韩另类美女| 日韩中文在线电影| 你懂的国产精品永久在线| 蜜臀va亚洲va欧美va天堂| 韩国精品主播一区二区在线观看 | 亚洲欧洲一区二区天堂久久| 另类综合日韩欧美亚洲| 亚久久调教视频| 欧洲激情综合| 亚洲天堂资源| 免费日韩成人| 日韩精品欧美大片| 午夜久久一区| 色老板在线视频一区二区| 麻豆国产精品| 久久国际精品| 视频在线观看国产精品| 91成人网在线观看| 日韩三区免费| 福利一区二区三区视频在线观看| 69堂免费精品视频在线播放| 国产亚洲综合精品| 亚洲天堂黄色| 国产麻豆久久| 日韩中文在线电影| 国产欧洲在线| 国产999精品在线观看| 国产劲爆久久| 国产精品一区二区美女视频免费看| 亚洲精品进入| 亚洲精品国产精品粉嫩| 伊人影院久久| 婷婷综合亚洲| 国产一区亚洲| 欧美日韩国产免费观看视频| 欧美日韩一区二区综合| 久久免费黄色| 成人看片网站| 成人午夜国产| 久久中文字幕av| av高清一区| 久久亚洲成人| 国产综合激情| 免费日韩精品中文字幕视频在线| 亚洲欧洲午夜| 在线免费观看亚洲| 免费观看在线综合色| 日韩在线卡一卡二| 久久电影一区| 日韩精品中文字幕吗一区二区| 亚洲综合福利| 日韩国产欧美视频| 久久不见久久见免费视频7| 久久99蜜桃| 国产不卡精品在线| 蜜桃av在线播放| 天堂网av成人| 国产亚洲毛片| 亚洲综合色婷婷在线观看| 日韩精选在线| 国产精品一区二区三区四区在线观看 | 欧美.日韩.国产.一区.二区 | 在线视频亚洲| 亚洲免费观看高清完整版在线观| 日韩影院精彩在线| 欧美日韩午夜电影网| 国产精品15p| 日韩国产欧美一区二区| 99视频精品全国免费| 精品91久久久久| 日韩**一区毛片| 麻豆国产精品视频| 99国产精品免费视频观看| 欧美日韩国产高清| 亚洲三级视频| 国产精品夜夜夜| а√在线中文在线新版| 亚洲a在线视频| 日韩一区二区久久| 日本不卡高清| 精品视频一区二区三区四区五区| 免费福利视频一区二区三区| 日韩午夜免费| 日韩av一级片| 国产91在线精品| 亚洲深爱激情| 欧美一级二区| 亚洲三级欧美| 视频一区国产视频| 国产精品毛片久久久| 国产中文在线播放| 国产亚洲午夜| 国产精品蜜月aⅴ在线| 午夜精品久久久久久久久久蜜桃| 免费久久精品视频| 精品无人区麻豆乱码久久久| 欧美亚洲国产一区| 青青国产精品| 99久久久国产精品美女| 日韩亚洲精品在线观看| 91一区二区| 免费成人性网站| 精品国产一区二区三区av片| 不卡一区综合视频| 国产日产高清欧美一区二区三区| 日产精品一区| 日韩欧美激情电影| 日韩中文在线电影| 日韩精品欧美成人高清一区二区| 日韩国产激情| 日韩成人精品一区二区三区| 亚洲天堂资源| 四虎精品永久免费| 日本久久成人网| 久久国内精品视频| 亚洲精品中文字幕乱码| 国产劲爆久久| 老牛国产精品一区的观看方式| 精品久久美女| 日韩一区欧美二区| 欧洲一区二区三区精品| 91成人精品在线| 在线国产一区| 久久午夜影院| 日本v片在线高清不卡在线观看| 久久视频一区| 麻豆成人av在线| 亚洲欧美久久精品| 九色精品91| 日本欧美国产| 88久久精品| 欧美在线综合| 91精品国产福利在线观看麻豆| 国产精成人品2018| 色狠狠一区二区三区| 久久人人88| 国产在视频一区二区三区吞精| 偷拍亚洲精品| 欧美一级精品| 国产一区二区精品久| 日韩精品成人在线观看| 国产亚洲精品v| 久久久影院免费| 成人国产精品一区二区免费麻豆| 日本欧美一区二区|