Good day. There was a need to wrap the image (ImageView) text (TextView). Googling a little bit, I managed to find a good class that solves my problem, because you can’t do anything sensible using standard tools. But still not so simple. If the text does not contain line breaks, then everything is in order, but I have at least 40 hyphenations in the text. Thus, for the first lines, everything works as it should, for subsequent ones containing '\ n', there is an indent from the other end (by default from the left, and with a line break - from the right). Thus, the line full of porridge. Please help somehow to settle the situation. Thank you in advance.
1 answer
With two TextView can be easier. Those. One textview is placed to the right of the image, the second - under it. It remains only to pick up the height so that a clear number of lines fit (margin for the picture, for example).
TextView mFirst = ... TextView mSecond = ... mFirst.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressLint("NewApi") @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT >= 16) { // API 11 mFirst.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { mFirst.getViewTreeObserver().removeGlobalOnLayoutListener(this); } int h = mFirst.getHeight(); int lh = mFirst.getLineHeight(); int maxl = h / lh; if (mFirst.getLineCount() > maxl) { int i = mFirst.getLayout().getLineStart(maxl); String text = (String) mFirst.getText(); mSecond.setText(text.substring(i)); mFirst.setText(text.substring(0, (i == 0) ? 0 : i - 1)); } else { mSecond.setText(""); } } }); Threat Code torn from different parts of the project, errors may occur.
- Thank you, I tried to write something similar yesterday, but in the end nothing came of it. I'm going to try to attach to the project.)) _____ Bedapchal ... getLineCount () == 0, because I have an adapter. ((Seemingly, no luck. - PhoEn-X
- @ PhoEn-X, try executing the code after the inflate for the view (in the getView adapter) when the dimensions are already calculated. - Yura Ivanov
- @Yura Ivanov, but have you heard about the Spannable class and its derivatives? Here is the correct answer. - Helisia
- @SuperCreeper, did you read the question? Link to the same snippet and given. In addition, in terms of convenience, the version with webView looks much more interesting if you generate content in the code. ____ @ PhoEn-X, sketched a gist with an adapter, everything works. - Yura Ivanov
- @Yura Ivanov, thanks, of course, but I went artisanal: I folded all the indents, created exactly the same TextView, set indents, got the right width, set the text, read the position where the required line ends, and then just sent this value to adapter constructor)) Thus, I managed to keep all the previously set span (for I have about 50 in total). And for some reason I did not work in the adapter, although the code was executed after setting the text. But still, your answer really helped to do everything with 1 TextView, and everything works. Thanks again! Rescued)) - PhoEn-X
|