【每日积累】20141108 ActionBar.Tab的背景设置

来源:互联网 发布:java排序库函数 编辑:程序博客网 时间:2024/06/08 12:31

Actionbar的tab界面显示效果由应用主题中的actionBarTabStyle 属性来控制,如果当前应用使用Theme.Holo.Light主题,那么默认使用的style就是Widget.Holo.Light.ActionBar.TabView,下面为此样式的代码:

<style name="Widget.Holo.ActionBar.TabView" parent="Widget.ActionBar.TabView">        <item name="android:background">@drawable/tab_indicator_ab_holo</item>        <item name="android:paddingStart">16dip</item>        <item name="android:paddingEnd">16dip</item>    </style>

当前样式针对每一个TabView的显示背景,显示内容填充进行了定义,我们要实现Tab的点击效果就重定义其BackGround属性就可以了,从源代码中可以看到,

tab_indicator_ab_holo文件内容为:
<pre name="code" class="html"><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- Non focused states -->    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@color/transparent" />    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" />    <!-- Focused states -->    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/list_focused_holo" />    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" />    <!-- Pressed -->    <!--    Non focused states -->    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/list_pressed_holo_dark" />    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />    <!--    Focused states -->    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" /></selector>

xml文件针对其焦点获得、失去,是否为选中状态,是否为按下状态进行判断,来确定显示的效果。

我们可以根据个人需要替换图片或其他资源,在自己的应用中配置styles.xml,所用Tab的Activity使用的Theme需要配置actionBarTabStyle属性,并且需要自定义一个继承自Widget.Holo.ActionBar.TabView样式的style配置到actionBarTabStyle属性中,在自定义的style中,配置自定义的drawable资源到android:background属性上。


到目前为止,简单的一个Tab状态变化效果就做好了。


前段时间有个需求,需要实现带有纹理的Tab效果,因为TabView的添加都是动态添加,因此需要做.9图片,但是有纹理的又会去拉伸,纠结了好久,今天找到解决方案:

将纹理作为ActionBar的background,配置到actionBarTabBarStyle属性定义的Widget.Holo.ActionBar.TabBar样式中,原生配置为:

<style name="Widget.Holo.ActionBar.TabBar" parent="Widget.ActionBar.TabBar">        <item name="android:divider">?android:attr/actionBarDivider</item>        <item name="android:showDividers">middle</item>        <item name="android:dividerPadding">12dip</item>    </style>
增加<item name="android:background">@drawable/tab_bg</item>配置,(tab_bg为自定义的drawable:

<bitmapxmlns:android="http://schemas.android.com/apk/res/android"android:src="@drawable/palyer"android:tileMode="repeat"></bitmap>

使用平铺的方式将player作为ActionBar的背景

)


这样在TabView的图片定义中,只要将选中状态的部分区域置为透明,就可以实现在选中时看到背景的纹理效果了。



TabView的控制项比较多,针对Divider,maxHeight,minWidth等都有控制,如果用到相关的,可以进行设置,OK,今天的TAB工作结束。


0 0
原创粉丝点击