自定义控件定义样式
首先在attrs中定义属性:
1 | <?xml version="1.0" encoding="utf-8"?> |
2 |
3 | <resources> |
4 | <declare-styleable name="tabStyleable"> |
5 | <attr name="tabPageIndicatorStyle" format="reference"/> |
6 | <attr name="tabTextStyle" format="reference"/> |
7 | </declare-styleable> |
8 | |
9 | </resources> |
然后使用这些属性:
1 | <!-- Application theme. --> |
2 | <style name="AppTheme" parent="AppBaseTheme"> |
3 | <!-- tab的样式 --> |
4 | <item name="tabPageIndicatorStyle">@style/Widget.TabPageIndicator</item> |
5 | <item name="tabTextStyle">@style/Widget.TabPageIndicator.Text</item> |
6 | </style> |
最后定义样式:
01 | <?xml version="1.0" encoding="utf-8"?> |
02 |
03 | <resources> |
04 | |
05 | <style name="Widget"></style> |
06 |
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> |
14 | </style> |
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> |
21 | </style> |
22 | |
23 | <style name="TextAppearance.TabPageIndicator" parent="Widget"> |
24 | |
25 | </style> |
26 | </resources> |
最后指定什么控件使用这个属性和样式:
01 | <?xml version="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> |

浙公网安备 33010602011771号