原帖: link.
项目中有这种需求,google了下发现了解决方案,摘录如下方便大家排查. 原文链接
原理是利用selector
第一步 在drawable文件夹下建立文件 tab_background.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/tab_background_selected" android:state_selected="true" /> <item android:drawable="@drawable/tab_background_unselected" android:state_selected="false" android:state_focused="false" android:state_pressed="false" /> </selector>第二步 在drawable文件夹下建立选中效果的文件 tab_background_selected.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#d13fdd1a" /> </shape>第三步 在drawable文件夹下建立未选中效果的文件 tab_background_unselected.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#3F51B5" /> </shape>最后 建立一个style
<style name="Base.Widget.Design.TabLayout" parent="android:Widget"> <item name="tabBackground">@drawable/tab_background</item> <item name="tabIndicatorColor">#ff00ff</item> <item name="tabIndicatorHeight">2dp</item> </style>重点使用该属性<item name="tabBackground">@drawable/tab_background</item>
效果图
----淡定从容,宁静致远---- 分类: Android <div id="blog_post_info"> 好文要顶 关注我 收藏该文 青竹玉简 关注 - 0 粉丝 - 4 +加关注 0 0 <div class="clear"></div> <div id="post_next_prev"> <a href="https://www.cnblogs.com/matd/p/12734879.html" class="p_n_p_prefix">« </a> 上一篇: <a href="https://www.cnblogs.com/matd/p/12734879.html" title="发布于 2020-04-19 22:58">Android中代码申请权限(应对android6.0以上)</a> <br> <a href="https://www.cnblogs.com/matd/p/12855815.html" class="p_n_p_prefix">» </a> 下一篇: <a href="https://www.cnblogs.com/matd/p/12855815.html" title="发布于 2020-05-09 10:36">添加金额输入框校验</a> posted @ 2020-04-22 22:13 青竹玉简 阅读( 608) 评论( 0) 编辑 收藏 </div>