開發(fā)過程中經(jīng)常會遇到Tab左右或中間的圓角不一樣献酗,如下圖所示:
根據(jù)上圖發(fā)現(xiàn)兩個Tab圓角不同,且不對稱,無法通過設(shè)置tabBackground屬性來實現(xiàn)不同的圓角浪慌,解決方案是采用自定義TabView的方法笼恰。具體實現(xiàn)方案如下:
TabLayout屬性設(shè)置:
<android.support.design.widget.TabLayout
android:id="@+id/tb_layout"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
app:tabIndicatorHeight="0dp"
app:tabPaddingBottom="0dp"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabPaddingTop="0dp"
app:tabSelectedTextColor="@color/white"
app:tabTextAppearance="@style/text_14_33_df"/>
tabPadding和padding全部設(shè)置為0踊沸,由自定義TabView來設(shè)置相應(yīng)的padding,tabIndicatorHeight設(shè)置為0社证,tabSelectedTextColor設(shè)置選中Tab文字顏色雕沿,tabTextAppearance設(shè)置Tab未選中文字顏色。
左邊Tab選中背景bg_tab_left_select.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/tv_color_blue"/>
<corners
android:topLeftRadius="5dp"/>
</shape>
左邊Tab未選中背景bg_tab_left_unselect.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/white"/>
<corners
android:topLeftRadius="5dp"/>
</shape>
右邊Tab選中背景bg_tab_right_select.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/tv_color_blue"/>
<corners
android:topRightRadius="5dp"/>
</shape>
右邊Tab未選中背景bg_tab_right_unselect.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/white"/>
<corners
android:topRightRadius="5dp"/>
</shape>
左邊Tab背景選擇器tab_left_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_tab_left_unselect" android:state_selected="false"/>
<item android:drawable="@drawable/bg_tab_left_select" android:state_selected="true"/>
</selector>
右邊Tab背景選擇器tab_right_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_tab_right_unselect" android:state_selected="false"/>
<item android:drawable="@drawable/bg_tab_right_select" android:state_selected="true"/>
</selector>
左邊TabView自定義布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/tab_left_selector"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tab_text_left"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:gravity="center"
android:textSize="@dimen/text_size_14"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textColor="@color/tv_color_33"/>
</LinearLayout>
右邊TabView自定義布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/tab_right_selector"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tab_text_right"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:gravity="center"
android:textSize="@dimen/text_size_14"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textColor="@color/tv_color_33"/>
</LinearLayout>
注意:
自定義View的高度要保持與TabLayout高度一致猴仑,負責背景填充有問題审轮。
最后代碼實現(xiàn):
viewPager.setOffscreenPageLimit(fragments.size());
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setupWithViewPager(viewPager);
if (tabLayout != null) {
TabLayout.Tab tab1 = tabLayout.getTabAt(0);
if (tab1 != null) {
tab1.setCustomView(getTabView0(getActivity()));
}
TabLayout.Tab tab2 = tabLayout.getTabAt(1);
if (tab2 != null) {
tab2.setCustomView(getTabView1(getActivity()));
}
}
public View getTabView0(Context context) {
LayoutInflater mInflater = LayoutInflater.from(context);
View view = mInflater.inflate(R.layout.item_tab_left, null);
TextView tv = (TextView) view.findViewById(R.id.tab_text_left);
tv.setText(titles[0]);
return view;
}
public View getTabView1(Context context) {
LayoutInflater mInflater = LayoutInflater.from(context);
View view = mInflater.inflate(R.layout.item_tab_right, null);
TextView tv = (TextView) view.findViewById(R.id.tab_text_right);
tv.setText(titles[1]);
return view;
}