android button 字母自动变大写原因以及属性总结

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/android_freshman/article/details/53286818

最近在写例子的时候,发现写一个button的时候,定义text属性为字母的时候,字母自动变大写了,而使用textView 的时候却没有这种情况,所以研究和总结下;效果如下:

这里写图片描述


1.解决办法

设置 textAllCaps 为false 就可以了,如上图;


2.原因分析

首先我看了下我这个例子的编译项目的版本,项目版本为5.0

这里写图片描述

继续查看button 的源代码,使用的21 widget里面的button 属性

这里写图片描述


因为这个样式点不进去,所以网上查了资料,发现和版本有关,于是创建了一个4.4编译的项目版本

这里写图片描述

定义了xml 里面的button 属性 ,19的模拟器查看发现字母并没有大写,

这里写图片描述

继续查看源代码,引用的是4.4(19版本)的widget,
这里写图片描述

自此原因很明了是,是5.0以后button 的默认属性 textAllCaps 被改为true 了


3.原因总结

查看系统选代码:frameworks/base/core/res/res/values/styles_material.xml的第236行,代码如下:

<style name="TextAppearance.Material.Button">
        <item name="textSize">@dimen/text_size_button_material</item>
        <item name="fontFamily">@string/font_family_button_material</item>
        <item name="textAllCaps">true</item>
        <item name="textColor">?attr/textColorPrimary</item>
</style>

这里写图片描述

可以发现真的是把textAllCaps属性设置为true了。


4.附录TextView属性大全

因为 button 是继承 TextView 的,所以贴上 TextView 的属性

android:autoLink 是否自动链接网址或邮箱地址;
android:autoText 自动检测错误;
android:bufferType 决定getText()返回的类型
android:capitalize 指定使用类型
android:cursorVisible 光标是否可见
android:digits 数字输入
android:drawableBottom 内容显示在文字的下边
android:drawableEnd 内容显示在文字的结尾
android:drawableLeft 内容显示在文字的左边
android:drawablePadding 内容和文字之间的空隙
android:drawableRight 内容显示在文字的右边
android:drawableStart 内容显示在文字的开始
android:drawableTop 内容显示在文字的上边
android:editable 编辑功能,能够使用输入法
android:editorExtras 编辑功能扩展,用户设置
android:ellipsize 椭圆区域的显示方式
android:ems 可以在更多系统上运行
android:fontFamily 字体风格
android:freezesText 冻结在光标位置
android:gravity 文字小于显示范围时,x和y轴方向的调整
android:height 文字像素高度
android:hint 文本空白时的提示语
android:imeActionId 激活输入法ID序号
android:imeActionLabel 激活输入法符号
android:imeOptions 输入法操作
android:includeFontPadding 足够空间容纳字体显示
android:inputMethod 指定输入法
android:inputType 选择输入法
android:lineSpacingExtra 额外的文字间距
android:lineSpacingMultiplier 额外的文字间距,乘数
android:lines 多行显示
android:linksClickable 点击链接
android:marqueeRepeatLimit 跑马灯重复限制
android:maxEms 最大系统兼容
android:maxHeight 最大文本高度
android:maxLength 最大文本长度
android:maxLines 最大文本行数
android:maxWidth 最大文本长度
android:minEms 最小系统兼容
android:minHeight 最小文本高度
android:minLines 最小文本行数
android:minWidth 最小文本宽度
android:numeric 支持数字输入
android:password 文本作为密码
android:phoneNumber 支持电话号码输入
android:privateImeOptions 私人输入操作
android:selectAllOnFocus 点击全部选中
android:shadowColor 阴影颜色
android:shadowDx 阴影水平偏移
android:shadowDy 阴影垂直偏移
android:shadowRadius 阴影半径
android:singleLine 单行显示
android:text 显示文本
android:textAllCaps 文本全部大写
android:textAppearance 基本的文字颜色,字体,大小,风格
android:textColor 文本颜色
android:textColorHighlight 文本高亮颜色
android:textColorHint 文本提示颜色
android:textColorLink 链接文本颜色
android:textIsSelectable 文本能够被选中
android:textScaleX 水平缩放参数
android:textSize 文本大小
android:textStyle 文本风格
android:typeface 文本字体
android:width 文本宽度


5.参考资料

1.(Google)TextView属性大全(API-16)

猜你喜欢

转载自blog.csdn.net/android_freshman/article/details/53286818
今日推荐