Android AlertDialog 更改默认按钮大写的设定

android 自带的alertDialog 确实有它迷人的地方,自适配的给出你想要的符合主题色的按钮,也能自定义布局内容,但是底部按钮却为大写的设定。


查看源码发现dialog的布局


<Button android:id="@+id/button1"
    android:layout_width="0dip"
    android:layout_gravity="start"
    android:layout_weight="1"
    style="?android:attr/buttonBarButtonStyle"
    android:maxLines="2"
    android:layout_height="wrap_content" />

并没有设置textAllCaps

编辑这段代码: 添加android:textAllCaps是可以起效地 但是我们为了这么小的事情更改源码有点不好


所以我们可以在style上下功夫

1.搜索buttonBarButtonStyle 

找出value中用到的地方,可以看到定义为

<item name="buttonBarButtonStyle">@style/Widget.AppCompat.Button.ButtonBar.AlertDialog</item>

所以我们知道只要继承此父类,即可完成更改为按钮小写,也不影响其他功能


最后代码如下:


<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="buttonBarButtonStyle">@style/DialogButton</item>
</style>
<style name="DialogButton" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="textAllCaps">false</item>
</style>


我想大声赞一下 stackflow 答案和网上随便的博客不知高出多少节


如果你想用AlertDialog的原生样式,又想改变其背景颜色的解决办法


<style name="button_dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">?attr/app_bg</item>
</style>

在theme 下

<item name="buttonBarStyle">@style/button_dialog</item>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值