加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:27:50 所属栏目:百科 来源:网络整理
导读:Actionbar的tab界面显示效果由应用主题中的actionBarTabStyle 属性来控制,如果当前应用使用Theme.Holo.Light主题,那么默认使用的style就是Widget.Holo.Light.ActionBar.TabView,下面为此样式的代码: style name="Widget.Holo.ActionBar.TabView" parent=

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:
<bitmap
	xmlns: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工作结束。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读