TextView changed to match text

I am working on a custom view to resize text inside a text view to fit (I don't want to ellipse).

The problem I am facing is that when you resize the text, the text image itself is not overestimated. I looked at the source and saw that setTextSize () calls the following:

nullLayouts(); requestLayout(); invalidate(); 

Therefore, he had to overestimate. This may be a mistake, because it works fine on 2.3, and not on emulators 1.6, 1.5, and 2.1.

Here's a snippet of code, note that the text extension is expanded:

 @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if(w == 0 && h == 0) { setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultSize); } updateView(); } private void updateView() { int viewWidth = getViewWidth(); float textWidth = getTextWidth(); float textSize = textSize(); while(textWidth > viewWidth && textSize >= MIN_TEXT_SIZE) { setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize-1); textSize = getTextSize(); textWidth = getTextWidth(); } } 

Can someone give me a hint in the right direction to solve this problem?

+6
source share
1 answer

In your XML file, write the following code.

 <TextView android:layout_height="40dip" android:layout_width="100dip"/> 

In short, your xml file has a fixed size for your TextView.

-3
source

Source: https://habr.com/ru/post/885902/


All Articles