SoFunction
Updated on 2025-03-01

Android TextView removes the implementation method of adaptive default fontpadding

Android TextView removes the implementation method of adaptive default fontpadding

Recently, when I used textview in my project, I found that after using the Android:layout_height="wrap_content" property, the textview will have a default padding, that is, fontpadding. This will cause the spacing between the textview and other views to be larger than the ones you set. So how can we remove this spacing?

First, try setting includefontpadding=falseIf the goal cannot be achieved, you can follow the second method.

Second, implement a custom TextView, just inherit from TextView and rewrite the onDraw method.

FontMetricsInt fontMetricsInt; 
  @Override 
  protected void onDraw(Canvas canvas) { 
    if (adjustTopForAscent){//Set whether to remove the spacing, true is remove      if (fontMetricsInt == null){ 
        fontMetricsInt = new FontMetricsInt(); 
        getPaint().getFontMetricsInt(fontMetricsInt); 
      } 
      (0,  - ); 
    } 
    (canvas); 
  } 

The second method can generally achieve the goal. If it still cannot, you can only use marginTop to achieve the negative value, but this method is not recommended.

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading for helping you. Thank you for your support for this site!