|
29 | 29 | import android.text.StaticLayout; |
30 | 30 | import android.text.TextPaint; |
31 | 31 | import android.text.TextUtils; |
| 32 | +import android.text.method.AllCapsTransformationMethod; |
| 33 | +import android.text.method.TransformationMethod2; |
32 | 34 | import android.util.AttributeSet; |
33 | 35 | import android.view.Gravity; |
34 | 36 | import android.view.MotionEvent; |
@@ -91,6 +93,7 @@ public class Switch extends CompoundButton { |
91 | 93 | private ColorStateList mTextColors; |
92 | 94 | private Layout mOnLayout; |
93 | 95 | private Layout mOffLayout; |
| 96 | + private TransformationMethod2 mSwitchTransformationMethod; |
94 | 97 |
|
95 | 98 | @SuppressWarnings("hiding") |
96 | 99 | private final Rect mTempRect = new Rect(); |
@@ -207,6 +210,15 @@ public void setSwitchTextAppearance(Context context, int resid) { |
207 | 210 |
|
208 | 211 | setSwitchTypefaceByIndex(typefaceIndex, styleIndex); |
209 | 212 |
|
| 213 | + boolean allCaps = appearance.getBoolean(com.android.internal.R.styleable. |
| 214 | + TextAppearance_textAllCaps, false); |
| 215 | + if (allCaps) { |
| 216 | + mSwitchTransformationMethod = new AllCapsTransformationMethod(getContext()); |
| 217 | + mSwitchTransformationMethod.setLengthChangesAllowed(true); |
| 218 | + } else { |
| 219 | + mSwitchTransformationMethod = null; |
| 220 | + } |
| 221 | + |
210 | 222 | appearance.recycle(); |
211 | 223 | } |
212 | 224 |
|
@@ -526,8 +538,12 @@ public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
526 | 538 | } |
527 | 539 |
|
528 | 540 | private Layout makeLayout(CharSequence text) { |
529 | | - return new StaticLayout(text, mTextPaint, |
530 | | - (int) Math.ceil(Layout.getDesiredWidth(text, mTextPaint)), |
| 541 | + final CharSequence transformed = (mSwitchTransformationMethod != null) |
| 542 | + ? mSwitchTransformationMethod.getTransformation(text, this) |
| 543 | + : text; |
| 544 | + |
| 545 | + return new StaticLayout(transformed, mTextPaint, |
| 546 | + (int) Math.ceil(Layout.getDesiredWidth(transformed, mTextPaint)), |
531 | 547 | Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true); |
532 | 548 | } |
533 | 549 |
|
|
0 commit comments