How to auto size textview dynamically according to the length of the text in android?

前端 未结 7 806
不思量自难忘°
不思量自难忘° 2021-02-04 07:07

I am taking data of variable sizes from the server and setting to a textView and i wanted the textview to resize according to the length of the text set.

It is given in

7条回答
  •  无人共我
    2021-02-04 07:33

    Revision 26.0.1 of Support Library added support for autosizing in AppCompatTextView.

    Developers can now let the size of their text expand or contract automatically based on the size and characteristics of the TextView, making it much easier to optimize the text size on different screens or with dynamic content.

    Granularity

    In Java:

    Call the setAutoSizeTextTypeUniformWithConfiguration() method:

    setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)
    

    In XML:

    Use the autoSizeMinTextSize, autoSizeMaxTextSize, and autoSizeStepGranularity attributes to set the auto-sizing dimensions in the layout XML file:

    
    

    Preset Sizes

    In Java:

    Call the setAutoSizeTextTypeUniformWithPresetSizes() method:

    setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)
    

    In XML:

    Use the autoSizePresetSizes attribute in the layout XML file:

    
    

    To access the array as a resource, define the array in the res/values/arrays.xml file:

    
        10sp
        12sp
        20sp
        40sp
        100sp
    
    

提交回复
热议问题