-
Notifications
You must be signed in to change notification settings - Fork 91
Style Appliers and Builders
Ordinarily views are styled via a generated Paris class. Calling myView.style(...)
automagically works regardless of the view type. In most cases that's all you need to know. For advanced usages it can be useful to understand what's going on under the hood.
For each styleable view Paris generates a StyleApplier
implementation. For MyView
the style applier would be named MyViewStyleApplier
and the package is always the same as the view. This applies to custom views and to the supported Android views (for example a ViewStyleApplier
class is generated).
Style appliers are responsible for applying style attribute values to views of their type. They also delegate to the style applier of the parent view type when necessary.
Whenever myView.style(...)
is called, a new instance of MyViewStyleApplier
is used:
myView.style(R.style.MyStyle)
// Is equivalent to
MyViewStyleApplier(myView).apply(R.style.MyStyle)
Click to see the example in Java.
Paris.style(myView).apply(R.style.MyStyle);
// Is equivalent to
new MyViewStyleApplier(myView).apply(R.style.MyStyle);
Similarly, a StyleBuilder
implementation is generated as a nested class in each StyleApplier
class. For MyView
the style builder would be named MyViewStyleApplier.StyleBuilder
.
Style builders let you build complex styles that can be applied by style appliers.
Whenever Paris.styleBuilder(myView)
is called, a new instance of MyViewStyleApplier.StyleBuilder
is returned:
Style style = Paris.styleBuilder(myView)
.add(R.style.MyStyle)
.build()
// Is equivalent to
Style style = new MyViewStyleApplier.StyleBuilder(myView)
.add(R.style.MyStyle)
.build();
The Kotlin implementation of the style builders is completely different from the Java one. It uses the generic ExtendableStyleBuilder<T>
class and extension functions for each attribute:
val style = myViewStyle {
add(R.style.MyStyle)
}
// Is equivalent to
val style = ExtendableStyleBuilder<MyView>().run {
add(R.style.MyStyle)
build()
}