How to adjust text font size to fit textview

后端 未结 22 1775
無奈伤痛
無奈伤痛 2020-11-22 05:15

Is there any way in android to adjust the textsize in a textview to fit the space it occupies?

E.g. I\'m using a TableLayout and adding several Te

22条回答
  •  再見小時候
    2020-11-22 06:04

    Works with modification

    You need to set the text view size like this because otherwise setTextSize assumes the value is in SP units:

    setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
    

    And you needed to explicitly add this code.

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        refitText(this.getText().toString(), parentWidth);
    }
    

提交回复
热议问题