I'm working on a custom view to resize the text inside a textview so it'll fit (i don't want to ellipsize).
The trouble i'm having is that when changing the text size, the textview itself is not remeasured. I've been looking at the source and saw that the setTextSize() is calling the following:
nullLayouts();
requestLayout();
invalidat开发者_StackOverflow社区e();
So it should've remeasured. This could be a bug because it works fine on 2.3 and not on 1.6, 1.5 and 2.1 emulators.
Here's piece of the code, note that textview is extended:
@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 anyone give me hint in the right direction to solve this issue?
In your xml file write the following code.
<TextView
android:layout_height="40dip"
android:layout_width="100dip"/>
In short, in your xml file set the fixed size of your TextView.
精彩评论