我的两个标签有这些代码。我想改变颜色,但我不知道该怎么做。它应该在我的java文件或我的xml中完成吗?谢谢
这是我的代码
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
// This is now the first activity that you see when you enter the app, it derives from TabActivity
public class TabsActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// The activity displays a TabHost layout with a TabWidget below the actual tab contents
setContentView(R.layout.tabs);
// Two tabs are added one displays the main list activity, the other an info activity
addNewTab("Kalender", MainActivity.class);
addNewTab("Info", InfoActivity.class);
}
// This function defines and adds a tab to the interface
private void addNewTab(String name, Class<? extends Activity> activityClass)
{
TabHost tabHost = getTabHost();
// The new tab will display a separate activity, so it needs an intent for it
Intent activityIntent = new Intent().setClass(this, activityClass);
// The TabSpec sets the internal name and the visible name of the newly created tab
TabHost.TabSpec spec = tabHost.newTabSpec(name).setIndicator(name).setContent(activityIntent);
// Finally, the new tab is added to the TabHost
tabHost.addTab(spec);
}
}
答案 0 :(得分:2)
更改TAB的文字颜色和背景颜色
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE); //Changing background color of tab
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); /*for Selected Tab changing text color*/
tv.setTextColor(Color.BLACK);
}
答案 1 :(得分:0)
如果要自定义选项卡的外观,则应使用自己的选项卡窗口小部件。问题是大多数Android小部件都是使用位图为主题的,所以你不能简单地改变渐变色。
有些人建议只更改标准小部件的backgroundColor,但它看起来会相当扁平。
使用您自己的小部件something like this:
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
答案 2 :(得分:0)
这是使单个标签的背景具有颜色的一种方法,也可以设置一种颜色。
tabHost.getTabWidget().getChildAt(tabIndex).setBackgroundColor(color);
答案 3 :(得分:0)
嘿,如果您要更改标签颜色,就像在Google Playstore中一样,请尝试以下操作:
public class MainActivity extends AppCompatActivity implements TabLayout.BaseOnTabSelectedListener {
private AppBarLayout appBarLayout;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private View mRevealView;
private View mRevealBackgroundView;
private int fromColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appBarLayout = findViewById(R.id.main_appbar);
toolbar = findViewById(R.id.main_toolbar);
tabLayout = findViewById(R.id.main_tablayout);
viewPager = findViewById(R.id.main_viewPager);
mRevealView = findViewById(R.id.reveal);
mRevealBackgroundView = findViewById(R.id.revealBackground);
setUpTabs();
setSupportActionBar(toolbar);
fromColor = R.color.colorTabOne;
}
private void setUpTabs() {
viewPager.setAdapter(new ViewPagerAdapter());
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(this);
tabLayout.getTabAt(0).setText("TAB ONE");
tabLayout.getTabAt(1).setText("TAB TWO");
tabLayout.getTabAt(2).setText("TAB THREE");
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case 0:
animateAppAndStatusBar(0, R.color.colorTabOne);
break;
case 1:
animateAppAndStatusBar(appBarLayout.getWidth() / 2, R.color.colorTabTwo);
break;
case 2:
animateAppAndStatusBar(appBarLayout.getWidth(), R.color.colorTabThree);
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
private void animateAppAndStatusBar(int cx, final int toColor) {
Animator animator = ViewAnimationUtils.createCircularReveal(
mRevealView,
cx,
appBarLayout.getBottom(), 0,
appBarLayout.getWidth() / 2);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
mRevealView.setBackgroundColor(getResources().getColor(toColor));
}
});
mRevealBackgroundView.setBackgroundColor(getResources().getColor(fromColor));
animator.setStartDelay(200);
animator.setDuration(125);
animator.start();
mRevealView.setVisibility(View.VISIBLE);
fromColor = toColor;
}
class ViewPagerAdapter extends FragmentPagerAdapter {
ViewPagerAdapter() {
super(MainActivity.this.getSupportFragmentManager());
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new TabOneFragment();
case 1:
return new TabTwoFragment();
case 2:
return new TabThreeFragment();
default:
throw new IllegalArgumentException("Invalid position " + i);
}
}
@Override
public int getCount() {
return 3;
}
}
}