Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between StaticLayout and DynamicLayout

Tags:

android

layout

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?

like image 479
cheesus Avatar asked Oct 31 '14 10:10

cheesus


1 Answers

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();
like image 90
Maxim Paliy Avatar answered Sep 20 '22 08:09

Maxim Paliy