1

In my job,our UI always mark the font size of textView (aTextView) in sp (or px);In our case another view(as bView) is below aTextView,the aTextView's real height will effect bView's position;this will change the overall layout with little difference.

I want to know the real height of a textview in different textSize,when we use wrap_content set the TextView's layout_height .We can discuss this problem in a simple way that 1px=1sp.

lichao
  • 29
  • 5
  • Did you check this: https://stackoverflow.com/questions/3779173/determining-the-size-of-an-android-view-at-runtime ? – sdabet Feb 16 '17 at 06:50

3 Answers3

0

here with this method you can identify the textview height

public static int getHeight(Context context, String text, int textSize, int deviceWidth) {
   TextView textView = new TextView(context);
   textView.setText(text);
   textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
   int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.AT_MOST);
   int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
   textView.measure(widthMeasureSpec, heightMeasureSpec);
   return textView.getMeasuredHeight();
}

If textSize is not given in pixels, change the first paramter of setTextSize().

copied from How to Measure TextView Height Based on Device Width and Font Size?

Community
  • 1
  • 1
Ankush Bist
  • 1,862
  • 1
  • 15
  • 32
  • thanks for this method,my intention is to know padding on top and bottom in a official info. – lichao Feb 16 '17 at 07:11
0

From my experience, a TextView with text size of 16sp/16dp will have a height of 18dp, i.e 16dp + 1dp padding on top and bottom.

Be aware that this is just an estimate. The actual height may or may not depend on a lot of other factors like font and fontsize etc. To check, you can just put a square View next to it in your layout or check by code.

I will post here if I happen to find any official info on how TextViews are sized.

ShahiM
  • 3,179
  • 1
  • 33
  • 58
  • yean ,I had measure it just in your conclusion but as an estimate.Wait for your official info that's what I need.I have find it for a long time. – lichao Feb 16 '17 at 07:13
0

Using Constraint Layout can solve your problem.Give it a try.Image

TextView B is vertically constrained to the ImageView and you can do the vice versa. constrained item will automatically resize according to the item it is constrained.