I understand that DynamicLayout
is intended for a Layout that has its text being changed, whereas StaticLayout
deals with an immutable text.
However, the two classes mostly have the same methods, according to their docs:
http://developer.android.com/reference/android/text/StaticLayout.html http://developer.android.com/reference/android/text/DynamicLayout.html
Specifically, I don't see a method setText()
on the DynamicLayout
class.
What is their difference, and how can I use DynamicLayout
to calculate layouts where the text ist changing?
In order to be updated on text change, DynamicLayout expects Spannable as first parameter. In that case it creates instance of internal static class DynamicLayout.ChangeWatcher, and attaches it to the Spannable. The Spannable, in turn, needs to implement Editable in order to be updated.
Example:
SpannableStringBuilder base = new SpannableStringBuilder("a");
DynamicLayout dynamicLayout = new DynamicLayout(base, base, paint, width, Alignment.ALIGN_NORMAL, 1.0, 0, true);
int firstHeight = dynamicLayout.getHeight();
base.append("\nA");
int secondHeight = dynamicLayout.getHeight();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With