Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

我的 Android 進階之旅:Button 上的英文字串自動變大寫,怎麼解?

編輯:關於 Android 程式設計

今天碰到一個關於 Button 的問題:Button 上的英文字串會自動變成大寫,執行環境是 Android 5.1。

圖 1:Button

圖 2:TextView

Button 的定義如下:

<Button
    android:id="@+id/addContacts"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="@string/contactList_addContact" />

TextView 的定義如下:

<TextView
    android:gravity="center"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:text="@string/contactList_addContact"
    android:textSize="24sp" />

兩者引用的是同一個字串 contactList_addContact:

<string name="contactList_addContact">Add</string>

結果 Button 顯示 ADD,TextView 顯示 Add。之前還真的沒遇到過,有點悶。更奇怪的是其他 Button 顯示都正常:

這兩個按鈕的定義是:

<LinearLayout
    android:gravity="center"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:orientation="horizontal">

    <Button
        android:gravity="center"
        android:id="@+id/btn_cancel"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="@string/btn_cancel"
        android:textColor="@color/black" />

    <Button
        android:gravity="center"
        android:id="@+id/btn_save"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="@string/btn_save"
        android:textColor="@color/black" />
</LinearLayout>

引用的字串是:

<string name="btn_cancel">Cancel</string>
<string name="btn_save">Save</string>

這兩個之所以「正常」,其實只是因為 Cancel 與 Save 這兩個字的確沒有大小寫混排的問題——它們本來就被強制全大寫了,只是不容易被察覺。

原因:Material 主題把 textAllCaps 打開了

猜測是 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。這是 Material Design 規範的要求:按鈕文字一律使用大寫,以維持視覺一致性。所以這不是 bug,而是設計決定。

解法

如果設計上必須保留原本的大小寫,只要在該按鈕的定義中加上 android:textAllCaps="false" 即可。這個屬性用來控制是否以全大寫呈現文字:

<Button
    android:id="@+id/addContacts"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="@string/contactList_addContact"
    android:textAllCaps="false" />

改完後的效果:

補充幾個相關做法,視情況挑一種即可:

  • 全域關閉:在 App 主題中加上 <item name="android:textAllCaps">false</item>,讓所有 Button 都恢復原樣。
  • 程式碼中設定:呼叫 button.setAllCaps(false),適合需要動態切換的場景。
  • 改用 TextView 並套用按鈕樣式,這樣能完全避開 Button 的預設 style,但相對費事。

順帶一提,textAllCaps 只改變繪製時的呈現,實際的 CharSequence 內容並沒有變。所以如果你的邏輯裡有比對 Button 文字字串的程式碼,不會因為這個屬性而受影響。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved