要动态修改TabLayout的背景颜色,你可以通过以下步骤来实现:
TabLayout tabLayout = findViewById(R.id.tabLayout);
setBackgroundColor()
方法:tabLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.new_background_color));
setTabBackground()
方法:tabLayout.setTabBackground(R.color.new_tab_background_color);
setTabTextColors()
方法:tabLayout.setTabTextColors(
ContextCompat.getColor(this, R.color.new_text_color),
ContextCompat.getColor(this, R.color.new_selected_text_color)
);
确保将上述代码中的R.color
替换为你自己的颜色资源。这样,你就可以动态修改TabLayout的背景颜色或整个TabLayout的背景颜色了。