简体   繁体   中英

Java android autosize Text in Edittext

This is my editText:

      <EditText
                android:id="@+id/max_deep_bin"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789"
                android:foregroundGravity="center"
                android:singleLine="true"
                android:gravity="center"
                android:inputType="numberDecimal"
                android:text="@string/zero"
                android:textSize="60sp"
                android:textStyle="bold" />

This edittext have to be in one line so I put a parameter : android:singleLine="true" but when a text is too long I want to change a textSize how I can do this ?

Use editText.setTextSize(...)

Parameter will be a float value size and the text size of editText will be set to size sp

If you want a different measurement unit other than sp, use a predefined integer value unit before the size parameter. What are those predefined values you ask..?

  • COMPLEX_UNIT_DIP : Device Independent Pixels (DP)
  • COMPLEX_UNIT_IN : Inches
  • COMPLEX_UNIT_MM : Millimetres
  • COMPLEX_UNIT_PT : Points
  • COMPLEX_UNIT_PX : Raw Pixels
  • COMPLEX_UNIT_SP : Scaled Pixels

For example, if you need the text size to be 20px, use

editText.setTextSize(COMPLEX_UNIT_PX, 20.0)

Hope this helps..!

Android introduces the auto text size increase option

 <?xml version="1.0" encoding="utf-8"?>
  <TextView
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:autoSizeTextType="uniform"
      android:autoSizePresetSizes="@array/autosize_text_sizes" />


    <resources>
      <array name="autosize_text_sizes">
        <item>10sp</item>
        <item>12sp</item>
        <item>20sp</item>
        <item>40sp</item>
        <item>100sp</item>
      </array>
    </resources>

    I Hope this will work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM