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.