首先在attrs中定义属性:
1 | <?xml "1.0" encoding= "utf-8" ?> |
2 |
3 | <resources> |
4 | <declare-styleable "tabStyleable" > |
5 | <attr "tabPageIndicatorStyle" format= "reference" /> |
6 | <attr "tabTextStyle" format= "reference" /> |
7 | </declare-styleable> |
8 | |
9 | </resources> |
然后使用这些属性:
1 | <!-- |
2 | <style "AppTheme" parent= "AppBaseTheme" > |
3 | <!-- |
4 | <item "tabPageIndicatorStyle" > @style /Widget.TabPageIndicator</item> |
5 | <item "tabTextStyle" > @style /Widget.TabPageIndicator.Text</item> |
6 | </style> |
最后定义样式:
01 | <?xml "1.0" encoding= "utf-8" ?> |
02 |
03 | <resources> |
04 | |
05 | <style "Widget" ></style> |
06 |
07 | <style "Widget.TabPageIndicator" parent= "Widget" > |
08 | <item "android:gravity" >center_horizontal</item> |
09 | <item "android:background" > @drawable /tab_indicator</item> |
10 | <item "android:paddingLeft" >22dip</item> |
11 | <item "android:paddingRight" >22dip</item> |
12 | <item "android:paddingTop" >12dp</item> |
13 | <item "android:paddingBottom" >12dp</item> |
14 | </style> |
15 | <style "Widget.TabPageIndicator.Text" parent= "Widget" > |
16 | <item "android:textAppearance" > @style /TextAppearance.TabPageIndicator</item> |
17 | <item "android:textColor" > @color /black</item> |
18 | <item "android:textSize" >12sp</item> |
19 | <item "android:textStyle" >bold</item> |
20 | <item "android:maxLines" > 1 </item> |
21 | </style> |
22 | |
23 | <style "TextAppearance.TabPageIndicator" parent= "Widget" > |
24 | |
25 | </style> |
26 | </resources> |
最后指定什么控件使用这个属性和样式:
01 | <?xml "1.0" encoding= "utf-8" ?> |
02 |
03 | <view |
04 | xmlns:android= "http://schemas.android.com/apk/res/android" |
05 | class = "com.demo.view.tab.TabView" |
06 | style= "?attr/tabPageIndicatorStyle" > |
07 | <TextView |
08 | android:id= "@android:id/text1" |
09 | android:layout_width= "wrap_content" |
10 | android:layout_height= "wrap_content" |
11 | android:gravity= "center" |
12 | style= "?attr/tabTextStyle" /> |
13 | </view> |
自定义控件定义样式,码迷,mamicode.com
时间: 2024-11-07 08:53:46