首先在attrs中定义属性:
1 |
<?xml
version= "1.0" encoding= "utf-8" ?> |
4 |
<declare-styleable
name= "tabStyleable" > |
5 |
<attr
name= "tabPageIndicatorStyle" format= "reference" /> |
6 |
<attr
name= "tabTextStyle" format= "reference" /> |
然后使用这些属性:
1 |
<!--
Application theme. --> |
2 |
<style
name= "AppTheme" parent= "AppBaseTheme" > |
4 |
<item
name= "tabPageIndicatorStyle" > @style /Widget.TabPageIndicator</item> |
5 |
<item
name= "tabTextStyle" > @style /Widget.TabPageIndicator.Text</item> |
最后定义样式:
01 |
<?xml
version= "1.0" encoding= "utf-8" ?> |
05 |
<style
name= "Widget" ></style> |
07 |
<style
name= "Widget.TabPageIndicator" parent= "Widget" > |
08 |
<item
name= "android:gravity" >center_horizontal</item> |
09 |
<item
name= "android:background" > @drawable /tab_indicator</item> |
10 |
<item
name= "android:paddingLeft" >22dip</item> |
11 |
<item
name= "android:paddingRight" >22dip</item> |
12 |
<item
name= "android:paddingTop" >12dp</item> |
13 |
<item
name= "android:paddingBottom" >12dp</item> |
15 |
<style
name= "Widget.TabPageIndicator.Text" parent= "Widget" > |
16 |
<item
name= "android:textAppearance" > @style /TextAppearance.TabPageIndicator</item> |
17 |
<item
name= "android:textColor" > @color /black</item> |
18 |
<item
name= "android:textSize" >12sp</item> |
19 |
<item
name= "android:textStyle" >bold</item> |
20 |
<item
name= "android:maxLines" > 1 </item> |
23 |
<style
name= "TextAppearance.TabPageIndicator" parent= "Widget" > |
最后指定什么控件使用这个属性和样式:
01 |
<?xml
version= "1.0" encoding= "utf-8" ?> |
05 |
class = "com.demo.view.tab.TabView" |
06 |
style= "?attr/tabPageIndicatorStyle" > |
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" /> |
自定义控件定义样式,码迷,mamicode.com