Android-用style修改AlertDialog修改按钮文字颜色
来源:互联网 发布:mac值是麻醉中什么 编辑:程序博客网 时间:2024/06/04 17:52
android系统为开发者提供了AlertDialog,用于快捷创建dialog。
用Builder模式创建,可以轻松设置title,message,取消,确定按钮点击事件等。但是本人实际项目用的比较少,因为跟UI设计的样式不太一样,按钮文字颜色不太好修改。经过一番查找,找到了修改按钮文字颜色的方法。AlertDialog最底层的构造方法:
/** * Construct an AlertDialog that uses an explicit theme. The actual style * that an AlertDialog uses is a private implementation, however you can * here supply either the name of an attribute in the theme from which * to get the dialog's style (such as {@link R.attr#alertDialogTheme}. */ protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) { super(context, resolveDialogTheme(context, themeResId)); mAlert = new AlertController(getContext(), this, getWindow()); }
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); a.recycle(); /* We use a custom title so never request a window title */ di.supportRequestWindowFeature(Window.FEATURE_NO_TITLE); }
可以看到各种自定义属性,是用R.styleable.AlertDialog获取的。打开android系统values搜索AlertDialog:
可以看到有一个style的属性,与AlertDialog的R.styleable.AlertDialog自定义属性是对应的,说明可以控制UI样式。
<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>
打开@layout/abc_alert_dialog_material:
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2015 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--><android.support.v7.widget.AlertDialogLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parentPanel" 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"/> <FrameLayout android:id="@+id/contentPanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="48dp"> <View android:id="@+id/scrollIndicatorUp" android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="top" android:background="?attr/colorControlHighlight" android:visibility="gone"/> <android.support.v4.widget.NestedScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v4.widget.Space android:id="@+id/textSpacerNoTitle" android:layout_width="match_parent" android:layout_height="@dimen/abc_dialog_padding_top_material" android:visibility="gone"/> <TextView android:id="@android:id/message" style="@style/TextAppearance.AppCompat.Subhead" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="?attr/dialogPreferredPadding" android:paddingRight="?attr/dialogPreferredPadding"/> <android.support.v4.widget.Space android:id="@+id/textSpacerNoButtons" android:layout_width="match_parent" android:layout_height="@dimen/abc_dialog_padding_top_material" android:visibility="gone"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> <View android:id="@+id/scrollIndicatorDown" android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="bottom" android:background="?attr/colorControlHighlight" android:visibility="gone"/> </FrameLayout> <FrameLayout android:id="@+id/customPanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="48dp"> <FrameLayout android:id="@+id/custom" 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"/></android.support.v7.widget.AlertDialogLayout>
最下面include了一个layout点进去:
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2014 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/buttonPanel" style="?attr/buttonBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true" android:scrollIndicators="top|bottom"> <android.support.v7.widget.ButtonBarLayout android: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:id="@android:id/button3" style="?attr/buttonBarNeutralButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <android.support.v4.widget.Space android:id="@+id/spacer" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" android:visibility="invisible"/> <Button android:id="@android:id/button2" style="?attr/buttonBarNegativeButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@android:id/button1" style="?attr/buttonBarPositiveButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </android.support.v7.widget.ButtonBarLayout></ScrollView>
button2和button1就是AlertDialog的取消和确定按钮。样式分别用了buttonBarNegativeButtonStyle和buttonBarPositiveButtonStyle两个style。搜索这两个style发现:
<item name="buttonBarStyle">@style/Widget.AppCompat.ButtonBar</item> <item name="buttonBarButtonStyle">@style/Widget.AppCompat.Button.ButtonBar.AlertDialog</item> <item name="buttonBarPositiveButtonStyle">?attr/buttonBarButtonStyle</item> <item name="buttonBarNegativeButtonStyle">?attr/buttonBarButtonStyle</item> <item name="buttonBarNeutralButtonStyle">?attr/buttonBarButtonStyle</item>
那么我们只需要在自己的主题重新定buttonBarNegativeButtonStyle和buttonBarPositiveButtonStyle两个style就可以修改按钮样式了。看下效果:
我修改的样式:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="buttonBarPositiveButtonStyle">@style/positiveBtnStyle</item> <item name="buttonBarNegativeButtonStyle">@style/negativeBtnstyle</item> </style> <!--确定按钮样式--> <style name="positiveBtnStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog"> <item name="android:textColor">#0000ff</item> </style> <!--取消按钮样式--> <style name="negativeBtnstyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog"> <item name="android:textColor">#999999</item> </style></resources>AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("你确定要把这5块钱捐给红十字会吗?") .setTitle("标题") .setNegativeButton("取消",null) .setPositiveButton("确定",null) .show();
阅读全文
0 0
- Android-用style修改AlertDialog修改按钮文字颜色
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- 修改 AlertDialog 中按钮颜色的方法
- 能修改按钮字体颜色的AlertDialog
- Android 按钮点击切换背景,同时修改文字颜色
- 修改navigationController返回按钮颜色和文字
- Android修改AlertDialog的背景颜色
- Android Spinner修改文字颜色
- Android学习【按钮颜色修改】
- 通过源码分析,修改AlertDialog按钮的颜色
- AlertDialog修改标题、内容、按钮的颜色和字体大小
- 修改CButton类按钮背景颜色和文字颜色
- 修改AlertDialog 按钮的背景
- Android修改ActionBar背景颜色和标题颜色的style
- Android TextInputLayout修改提示信息文字颜色
- Android TextInputLayout修改提示信息文字颜色
- bwdist
- 贪食蛇基础版
- Spring Boot 实现定时任务
- HBase学习笔记(一)HBase数据模型
- 《DOS命令一日通》第八章 自动文件执行(批处理文件)
- Android-用style修改AlertDialog修改按钮文字颜色
- 运维中心建设
- 每个人都应该知道的3种机器学习算法
- 「游戏引擎Mojoc」(5)快速指南
- find
- JAVA面向对象练习05第一种
- 我学习Android的一些套路
- 读书03《你的知识需要管理》
- LigerUI中LigerDialog.open的简易方法