別費(fèi)勁了输玷,用第三方庫(kù)! SmartTabLayout
使用支持更多自定義的SmartTabLayout.有我們想要的自定義下劃線寬度屬性撑瞧。
屬性 | 描述 |
---|---|
stl_indicatorAlwaysInCenter | 如果設(shè)置為true,選中的標(biāo)簽總是顯示在中心(如報(bào)刊亭的谷歌應(yīng)用程序),默認(rèn)為false |
stl_indicatorWithoutPadding | 如果設(shè)置為真,畫出沒(méi)有填充標(biāo)簽的指標(biāo),默認(rèn)為假 |
stl_indicatorInFront | 在前面的下劃線泞遗,默認(rèn)的假畫 |
stl_indicatorInterpolation | 指標(biāo)的行為:: ‘linear’ or ‘smart’ |
stl_indicatorGravity | 指示器的位置: ‘bottom’ or ‘top’ or ‘center’, default ‘bottom’ |
stl_indicatorColor | 指示劑顏色 |
stl_indicatorColors | 該指標(biāo)的多個(gè)顏色,可以設(shè)置每個(gè)標(biāo)簽的顏色 |
stl_indicatorThickness | 指標(biāo)的厚度 |
stl_indicatorWidth | 指標(biāo)的寬度(width), default ‘a(chǎn)uto’ |
stl_indicatorCornerRadius | 圓角半徑的指示器 |
stl_overlineColor | 頂線的顏色 |
stl_overlineThickness | 頂線厚度 |
stl_underlineColor | 底線的顏色 |
stl_underlineThickness | 底線的厚度 |
stl_dividerColor | 標(biāo)簽的顏色之間的分隔 |
stl_dividerColors | 制表符分隔的多個(gè)顏色席覆,可以設(shè)置每個(gè)標(biāo)簽的顏色 |
stl_dividerThickness | 間隔(divider)的厚度 |
stl_defaultTabBackground | 背景中每個(gè)選項(xiàng)卡史辙。一般來(lái)說(shuō),設(shè)置statelistdrawable |
stl_defaultTabTextAllCaps | 如果設(shè)置為真娜睛,所有標(biāo)簽的標(biāo)題將是大寫的髓霞,default true |
stl_defaultTabTextColor | 默認(rèn)的選項(xiàng)卡的文本顏色 |
stl_defaultTabTextSize | 默認(rèn)的選項(xiàng)卡的文本大小 |
stl_defaultTabTextHorizontalPadding | 默認(rèn)情況下包含的選項(xiàng)卡的文本布局填充 |
stl_defaultTabTextMinWidth | tab最小寬度 |
stl_customTabTextLayoutId | 布局標(biāo)識(shí)自定義選項(xiàng)卡。如果不指定布局畦戒,使用默認(rèn)選項(xiàng)卡 |
stl_customTabTextViewId | 自定義選項(xiàng)卡布局中的文本視圖標(biāo)識(shí)方库。如果你不確定customtabtextlayoutid,不工作 |
stl_distributeEvenly | 如果設(shè)置為真障斋,每個(gè)標(biāo)簽都有相同的權(quán)重, default false |
stl_clickable | 如果設(shè)置為假纵潦,請(qǐng)禁用選項(xiàng)卡的選擇, default true |
stl_titleOffset | 如果設(shè)置為“auto_center徐鹤,滑塊位置的標(biāo)簽中會(huì)不斷向中心。如果指定一個(gè)維度將它從左邊偏移邀层,默認(rèn)24dp |
stl_drawDecorationAfterTab | Draw the decoration(indicator and lines) after drawing of tab, default false 繪制標(biāo)簽后的裝飾(指標(biāo)和線) |
1. 添加依賴
// smarttablayout
implementation 'com.ogaclejapan.smarttablayout:library:1.6.1@aar'
2. xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".modifyTablayoutIndicator.MondifyActivity">
<com.ogaclejapan.smarttablayout.SmartTabLayout
android:id="@+id/smart_tablayout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
app:stl_clickable="true"
app:stl_customTabTextLayoutId="@layout/view_tab_text"
app:stl_distributeEvenly="true"
app:stl_dividerThickness="0dp"
app:stl_indicatorColor="@color/colorAccent"
app:stl_indicatorGravity="bottom"
app:stl_indicatorInterpolation="linear"
app:stl_indicatorThickness="2dp"
app:stl_indicatorWidth="20dp"
app:stl_overlineThickness="0dp"
app:stl_titleOffset="auto_center"
app:stl_underlineThickness="0dp">
</com.ogaclejapan.smarttablayout.SmartTabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3. SmartTabLayout中并沒(méi)有設(shè)置文本選中和未選中Tab時(shí)的效果返敬,我們可以通過(guò)app:stl_customTabTextLayoutId屬性引入自定義設(shè)置。
view_tab_text.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_text"
style="@style/Tabtext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp">
</TextView>
styles.xml
<style name="Tabtext">
<item name="android:textColor">@color/selector_tab_text</item>
<item name="android:textSize">20dp</item>
</style>
selector_tab_text.xml
在這里設(shè)置選中tab的字體顏色寥院,和未選中tab的字體顏色劲赠。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/holo_blue_light" android:state_selected="true" />
<item android:color="@android:color/holo_gray_light" />
</selector>
4. 因?yàn)橐昧俗远xTab布局,所以原生布局帶有的Tab點(diǎn)擊效果消失(類似于水波紋)秸谢。
我們可以在 view_tab_text.xml 文件中添加想要的水波紋效果凛澎。
android:foreground="@drawable/ripple_app_color"
ripple_app_color.xml: 水波紋效果
<?xml version="1.0" encoding="utf-8" ?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#292421">
</ripple>
- Activity
public class MondifyActivity extends AppCompatActivity {
private SmartTabLayout mSmartTablayout;
private ViewPager mViewPager;
private List<String> titles = new ArrayList<>();
private List<Fragment> fragments = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mondify);
initView();
initSet();
}
private void initView() {
mSmartTablayout = (SmartTabLayout) findViewById(R.id.smart_tablayout);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
}
private void initSet() {
titles.add("aaaa");
titles.add("bbbb");
titles.add("cccc");
titles.add("dddd");
titles.add("eeee");
titles.add("ffff");
for (int i = 0; i < titles.size(); i++) {
fragments.add(new BlankFragment());
}
MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mSmartTablayout.setViewPager(mViewPager);
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
}
6. 完成效果圖
自定義smartTabLayout.gif
7. 簡(jiǎn)單的SmartTabLayout布局
但是選項(xiàng)卡沒(méi)有定義選中后的顏色,必須自定義選項(xiàng)卡才可以
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".modifyTablayoutIndicator.SmartActivity">
<com.ogaclejapan.smarttablayout.SmartTabLayout
android:id="@+id/smartTabLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
app:stl_indicatorGravity="bottom"
app:stl_indicatorInterpolation="smart"
app:stl_indicatorThickness="2dp"
app:stl_distributeEvenly="true"
app:stl_dividerThickness="0dp"
app:stl_indicatorColor="@color/colorPrimary"
app:stl_indicatorWidth="20dp">
</com.ogaclejapan.smarttablayout.SmartTabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
效果圖
簡(jiǎn)單smartTabLayout.gif