android Button上面的英文字符串自动大写的问题解决

来源:互联网 发布:淘宝上传宝贝描述模版 编辑:程序博客网 时间:2024/04/30 02:14

    今天碰到一个关于Button的问题:android Button上面的英文字符串会自动变成大写,运行的Android 5.1版本,如下图所示:

图1:Button 

图2:TextView


这个Button的定义代码如下

[java] view plaincopy
  1. <Button  
  2.           android:id="@+id/addContacts"  
  3.           android:layout_width="match_parent"  
  4.           android:layout_height="wrap_content"  
  5.           android:text="@string/contactList_addContact" />  
TextView的定义代码如下

[java] view plaincopy
  1. <TextView  
  2.        android:layout_width="match_parent"  
  3.        android:layout_height="0dp"  
  4.        android:layout_margin="10dp"  
  5.        android:layout_weight="1"  
  6.        android:gravity="center"  
  7.        android:text="@string/contactList_addContact"  
  8.        android:textSize="24sp" />  

引用同一个字符串contactList_addContact,字符串内容如下:

[html] view plaincopy
  1. <string name="contactList_addContact">Add</string>  

但是Button显示出来就是ADD,而TextView显示出来就是Add。之前还真的没遇到过,郁闷。而且其他的Button都是显示正常,如下所示:

这两个按钮定义代码如下:

[java] view plaincopy
  1. <LinearLayout  
  2.         android:layout_width="fill_parent"  
  3.         android:layout_height="0dp"  
  4.         android:layout_margin="10dp"  
  5.         android:layout_weight="1"  
  6.         android:gravity="center"  
  7.         android:orientation="horizontal" >  
  8.   
  9.         <Button  
  10.             android:id="@+id/btn_cancel"  
  11.             android:layout_width="0dp"  
  12.             android:layout_height="fill_parent"  
  13.             android:layout_weight="1"  
  14.             android:gravity="center"  
  15.             android:text="@string/btn_cancel"  
  16.             android:textColor="@color/black" />  
  17.   
  18.         <Button  
  19.             android:id="@+id/btn_save"  
  20.             android:layout_width="0dp"  
  21.             android:layout_height="fill_parent"  
  22.             android:layout_weight="1"  
  23.             android:gravity="center"  
  24.             android:text="@string/btn_save"  
  25.             android:textColor="@color/black" />  
  26.     </LinearLayout>  

所引用的字符串为:

[html] view plaincopy
  1. <string name="btn_cancel">Cancel</string>  
  2. <string name="btn_save">Save</string>  


    好吧,以上就是问题的内容,至于为什么会有这个问题我估计是Android 5.1的SDK把Button的默认Style改了,样式默认把textAllCaps设置为true了,也没有去仔细研究。查看系统选代码:frameworks/base/core/res/res/values/styles_material.xml的第233行,代码如下:

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

    下面来说说怎么解决这个问题。只需要在Add按钮的定义中加上一个  android:textAllCaps="false"属性即可,该属性是用来设置是否使用大写字母来呈现文本。


即把代码改成如下:

[java] view plaincopy
  1. <Button  
  2.           android:id="@+id/addContacts"  
  3.           android:layout_width="match_parent"  
  4.           android:layout_height="wrap_content"  
  5.           android:textAllCaps="false"  
  6.           android:text="@string/contactList_addContact" />  

好吧,这样就解决了,改完后的效果图如下:


        


         ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

====================================================================================



0 0
原创粉丝点击