paris
2.0.2
Paris 允许您以编程方式定义样式并将其应用到 Android 视图,包括自定义属性。
在您项目的build.gradle
中:
dependencies {
implementation ' com.airbnb.android:paris:2.0.0 '
// Apply the Paris processor if you're using Paris annotations for code gen.
kapt ' com.airbnb.android:paris-processor:2.0.0 '
// or if you are using Kotlin Symbol Processing
ksp ' com.airbnb.android:paris-processor:2.0.0 '
}
要在库模块中使用 Paris,请参阅库模块。
myView.style( R .style. MyStyle )
Paris . style ( myView ). apply ( R . style . MyStyle );
其中myView
是任意视图实例, MyStyle
是XML定义的样式, style
是Paris提供的扩展函数。支持许多但不是所有属性,有关更多信息,请参阅支持的视图类型和属性。
myView.style {
add( R .style. StyleA )
add( R .style. StyleB )
…
}
Paris . styleBuilder ( myView )
. add ( R . style . StyleA )
. add ( R . style . StyleB )
…
. apply ();
如果存在一些重叠,则以最后添加的样式中的属性值为准。有关更多信息,请参阅组合样式。
textView.style {
// Using an actual value.
textColor( Color . GREEN )
// Or a resource.
textSizeRes( R .dimen.my_text_size_small)
}
Paris . styleBuilder ( textView )
// Using an actual value.
. textColor ( Color . GREEN )
// Or a resource.
. textSizeRes ( R . dimen . my_text_size_small )
. apply ();
也可以与样式资源结合使用:
textView.style {
// Adds all the attributes defined in the MyGreenTextView style.
add( R .style. MyGreenTextView )
textSizeRes( R .dimen.my_text_size_small)
}
Paris . styleBuilder ( textView )
// Adds all the attributes defined in the MyGreenTextView style.
. add ( R . style . MyGreenTextView )
. textSizeRes ( R . dimen . my_text_size_small )
. apply ();
有关更多信息,请参阅以编程方式定义样式。
属性声明如下:
< declare-styleable name = " MyView " >
< attr name = " title " format = " string " />
< attr name = " image " format = " reference " />
< attr name = " imageSize " format = " dimension " />
</ declare-styleable >
自定义视图使用@Styleable
和@Attr
进行注释:
// The value here corresponds to the name chosen in declare-styleable.
@Styleable( " MyView " )
class MyView (…) : ViewGroup(…) {
init {
// This call enables the custom attributes when used in XML layouts. It
// extracts styling information from AttributeSet like it would a StyleRes.
style(attrs)
}
@Attr( R .styleable. MyView_title )
fun setTitle ( title : String ) {
// Automatically called with the title value (if any) when an AttributeSet
// or StyleRes is applied to the MyView instance.
}
@Attr( R .styleable. MyView_image )
fun setImage ( image : Drawable ? ) {
// Automatically called with the image value (if any) when an AttributeSet
// or StyleRes is applied to the MyView instance.
}
@Attr( R .styleable. MyView_imageSize )
fun setImageSize (@Px imageSize : Int ) {
// Automatically called with the imageSize value (if any) when an
// AttributeSet or StyleRes is applied to the MyView instance.
}
}
// The value here corresponds to the name chosen in declare-styleable.
@ Styleable ( "MyView" )
public class MyView extends ViewGroup {
public MyView ( Context context ) {
super ( context );
}
public MyView ( Context context , AttributeSet attrs ) {
this ( context , attrs , 0 );
}
public MyView ( Context context , AttributeSet attrs , int defStyle ) {
this ( context , attrs , defStyle );
// This call enables the custom attributes when used in XML layouts. It
// extracts styling information from AttributeSet like it would a StyleRes.
Paris . style ( this ). apply ( attrs );
}
@ Attr ( R . styleable . MyView_title )
public void setTitle ( String title ) {
// Automatically called with the title value (if any) when an AttributeSet
// or StyleRes is applied to the MyView instance.
}
@ Attr ( R . styleable . MyView_image )
public void setImage ( Drawable image ) {
// Automatically called with the image value (if any) when an AttributeSet
// or StyleRes is applied to the MyView instance.
}
@ Attr ( R . styleable . MyView_imageSize )
public void setImageSize ( @ Px int imageSize ) {
// Automatically called with the imageSize value (if any) when an
// AttributeSet or StyleRes is applied to the MyView instance.
}
}
当使用AttributeSet
扩展视图或应用样式时,Paris 将调用@Attr
注释的方法。
有关更多信息,请参阅自定义视图属性。
对于我们希望能够设置样式的 2 个子视图,属性声明如下:
< declare-styleable name = " MyHeader " >
< attr name = " titleStyle " format = " reference " />
< attr name = " subtitleStyle " format = " reference " />
...
</ declare-styleable >
子视图字段用@StyleableChild
注释:
@Styleable( " MyHeader " )
class MyHeader (…) : ViewGroup(…) {
@StyleableChild( R .styleable. MyHeader_titleStyle )
internal val title : TextView …
@StyleableChild( R .styleable. MyHeader_subtitleStyle )
internal val subtitle : TextView …
init {
style(attrs)
}
}
@ Styleable ( "MyHeader" )
public class MyHeader extends ViewGroup {
@ StyleableChild ( R . styleable . MyHeader_titleStyle )
TextView title ;
@ StyleableChild ( R . styleable . MyHeader_subtitleStyle )
TextView subtitle ;
…
// Make sure to call Paris.style(this).apply(attrs) during initialization.
}
标题和副标题样式现在可以成为MyHeader
样式的一部分:
< MyHeader
...
app : titleStyle = " @style/Title2 "
app : subtitleStyle = " @style/Regular " />
myHeader.style {
// Defined in XML.
titleStyle( R .style. Title2 )
// Defined programmatically.
subtitleStyle {
textColorRes( R .color.text_color_regular)
textSizeRes( R .dimen.text_size_regular)
}
}
Paris . styleBuilder ( myHeader )
// Defined in XML.
. titleStyle ( R . style . Title2 )
// Defined programmatically.
. subtitleStyle (( builder ) -> builder
. textColorRes ( R . color . text_color_regular )
. textSizeRes ( R . dimen . text_size_regular ))
. apply ();
注意:诸如titleStyle
和subtitleStyle
之类的扩展函数是由 Paris 注释处理器在编译期间生成的。添加新的@StyleableChild
注释时,必须(重新)编译项目一次才能使相关功能可用。
有关更多信息,请参阅设置子视图样式。
@Styleable
class MyView (…) : View(…) {
companion object {
// For styles defined in XML.
@Style
val RED_STYLE = R .style. MyView_Red
// For styles defined programmatically.
@Style
val GREEN_STYLE = myViewStyle {
background( R .color.green)
}
}
}
@ Styleable
public class MyView extends View {
// For styles defined in XML.
@ Style
static final int RED_STYLE = R . style . MyView_Red ;
// For styles defined programmatically.
@ Style
static void greenStyle ( MyViewStyleApplier . StyleBuilder builder ) {
builder . background ( R . color . green );
}
}
为每个链接样式生成辅助方法:
myView.style { addRed() } // Equivalent to style(R.style.MyView_Red)
myView.style { addGreen() } // Equivalent to add(MyView.GREEN_STYLE)
myView.style {
addRed() // Equivalent to add(R.style.MyView_Red)
addGreen() // Equivalent to add(MyView.GREEN_STYLE)
…
}
Paris . style ( myView ). applyRed (); // Equivalent to apply(R.style.MyView_Red)
Paris . style ( myView ). applyGreen (); // No equivalent.
Paris . styleBuilder ( myView )
. addRed () // Equivalent to add(R.style.MyView_Red)
. addGreen () // No equivalent.
…
. apply ();
注意: addRed
和addGreen
等扩展函数是由 Paris 注释处理器在编译期间生成的。当添加新的@Style
注释时,必须(重新)编译项目一次才能使相关功能可用。
有关更多信息,请参阅将样式链接到自定义视图。
在巴黎维基上查看示例并浏览完整文档。
如果您仍有疑问,请随时创建新问题。
我们热爱贡献!查看我们的贡献指南并确保遵守我们的行为准则。
Copyright 2018 Airbnb, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.