Android TabLayout backgroud color 설정하기

TabLayout을 보면 Tab이 선택될때 배경을 바꿀 수 있는 함수등이 없다.

아래 방법을 사용하면 탭의 선택 유무에 따라 배경을 바꿀 수 있다.

  1. drawable 폴더에 tab_background.xml selector 생성
1
2
3
4
5
<?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>
  1. drawable 폴더에 tab_background_selected.xml 생성
1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#d13fdd1a" />
</shape>
  1. drawable 폴더에 tab_background_unselected.xml 생성
1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#3F51B5" />
</shape>
  1. styles.xml에 다음을 추가.
1
2
3
4
5
<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>

xml의 칼라 값을 변경함으로서 백그라운드 칼라를 설정할 수 있다.

Tab 배경색 변경

공유하기