TabLayout Custom View View 안에있는 TextView Selected 안될때 해결법!

2021. 2. 25. 16:02모바일/Android_Java

TabLayout Custom View View 안에있는 TextView Selected 안될때 해결법!

해결법

xml

  • drawable/tab_title_color
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/color04" />
    <item android:state_selected="true" android:color="@color/color04" />
    <item android:state_focused="true" android:color="@color/color04" />
    <item android:color="@color/color02" />
</selector>
  • Layout/design_tab_button.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_button"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dimen_35"
    android:clickable="true" >

    <TextView
        android:id="@+id/tab_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:text=""
        android:textColor="@drawable/hm_tab_title_color"
        android:gravity="center"
        android:textSize="15sp"
        android:textStyle="bold"
        android:singleLine="true"
        android:layout_centerInParent="true"
        android:duplicateParentState="true" />

    <View 
        android:id="@+id/tab_selected_under_line"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_4"
        android:background="@color/hm_tab_under_line_color"
        android:visibility="gone"
        android:layout_alignParentBottom="true"
        android:duplicateParentState="true" />

    <View
        android:id="@+id/tab_unselected_under_line" 
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_1"
        android:background="@color/color05"
        android:layout_alignParentBottom="true"
        android:duplicateParentState="true" />

</RelativeLayout>

자바코드

for(int i=0; i<TAB_TITLES.length;i++){
  tabs.getTabAt(i).setCustomView(createTabView(TAB_TITLES[i]));
  ColorStateList colorStateList = tabs.getTabTextColors();
  TextView textView = tabs.getTabAt(i).getCustomView().findViewById(R.id.tab_name);
  textView.setTextColor(colorStateList);
}