Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. @Override
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  3. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  4. int lineWidth = widthSize - getPaddingLeft() - getPaddingRight();
  5.  
  6. // Change the text size until the largest line of text fits.
  7. while (lineWidth < calculateTextWidth()) {
  8. for (TextView textView : this.childViews) {
  9. float newSize = textView.getTextSize() - 1;
  10. textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize);
  11. }
  12. }
  13.  
  14. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  15. }
  16.  
  17. <com.my.widgets.ScalingTextViewGroup
  18. android:layout_width="0dp"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. android:orientation="vertical"
  22. android:gravity="center_horizontal"
  23. >
  24. <TextView
  25. android:id="@+id/text_1"
  26. android:text="Text 1"
  27. android:textSize="20sp" />
  28. <TextView
  29. android:id="@+id/text_2"
  30. android:text="Text 2"
  31. android:textSize="18sp" />
  32. <TextView
  33. android:id="@+id/text_3"
  34. android:text="Text 3"
  35. android:textSize="18sp" />
  36. <TextView
  37. android:id="@+id/text_4"
  38. android:text="Text 4"
  39. android:textSize="18sp" />
  40. </com.my.widgets.ScalingTextViewGroup>
  41.  
  42. @Override
  43. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  44. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  45. if (widthMode != MeasureSpec.UNSPECIFIED) {
  46. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  47. int lineWidth = widthSize - getPaddingLeft() - getPaddingRight();
  48.  
  49. // Change the text size until the largest line of text fits.
  50. while (lineWidth < calculateTextWidth()) {
  51. for (TextView textView : this.childViews) {
  52. float newSize = textView.getTextSize() - 1;
  53. textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize);
  54. }
  55. }
  56. }
  57.  
  58. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement