實現(xiàn)方式也很簡單,為了使當前顏色改變,并使用動畫,我們需要在TabLayout.OnTabSelectedListener的監(jiān)聽中實現(xiàn)onTabSelected(TabLayout.Tab tab)方法。
為了使顏色改變有動畫效果,這里我使用了ValueAnimator,設置動畫時間和ArgbEvaluator來處理兩個ARGB顏色之間的動畫步驟。
具體方式:
public class UpdateToolbarColorActivity extends AppCompatActivity {
private TabLayout tabLayout;
private Toolbar toolbar;
private String[] colors = {"紅", "綠", "藍", "紫", "灰"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_toolbar_color_layout);
tabLayout = (TabLayout) findViewById(R.id.id_tab_layout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
for (String color : colors) {
tabLayout.addTab(tabLayout.newTab().setText(color));
}
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//獲取每次顏色的初始值
int colorFrom = ((ColorDrawable) toolbar.getBackground()).getColor();
int colorTo = getColorForTab(tab.getPosition());
//使顏色改變有動畫效果
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
//動畫執(zhí)行時間
colorAnimation.setDuration(1000);
//動畫監(jiān)聽器
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
int color = (int) animator.getAnimatedValue();
//修改toolbar背景顏色
toolbar.setBackgroundColor(color);
//修改tablayout背景顏色
tabLayout.setBackgroundColor(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//修改狀態(tài)欄背景顏色
getWindow().setStatusBarColor(color);
}
}
});
//執(zhí)行動畫
colorAnimation.start();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
/**
* 每個tab所對應的顏色值
*/
public int getColorForTab(int position) {
if (position == 0) return ContextCompat.getColor(this, R.color.red);
else if (position == 1) return ContextCompat.getColor(this, android.R.color.holo_green_light);
else if (position == 2) return ContextCompat.getColor(this, R.color.blue_color);
else if (position == 3) return ContextCompat.getColor(this, android.R.color.holo_purple);
else return ContextCompat.getColor(this, android.R.color.darker_gray);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/d_56"
android:background="@color/white"
></android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/id_tab_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#fff"
android:fillViewport="false"
app:tabMode="fixed"
app:layout_scrollFlags="scroll"
app:tabIndicatorColor="#d7e3"
app:tabIndicatorHeight="2dp"
app:tabSelectedTextColor="#d713"
app:tabTextColor="#ced0d3"
app:tabTextAppearance="@style/MyCustomTextAppearance"
>
</android.support.design.widget.TabLayout>
</android.support.design.widget.CoordinatorLayout>
好了,到這里就結束了,具體步驟就這么多。