1

我的两个标签都有这些代码。我想改变颜色,但我不知道怎么做。应该在我的 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);
}
}
4

4 回答 4

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);
    }
于 2012-06-18T06:00:48.507 回答
0

如果您想自定义标签的外观,您应该使用自己的标签小部件。问题是大多数 android 小部件都是使用位图主题的,所以你不能简单地改变渐变颜色。

有些人建议简单地更改标准小部件的背景颜色,但它看起来会相当平坦。

使用您自己的小部件是这样的:

// 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);

还可以查看Android 样式指南的选项卡部分

于 2012-06-16T21:00:16.880 回答
0

这是使单个选项卡的背景具有颜色的一种方法,也可以设置一种颜色。

tabHost.getTabWidget().getChildAt(tabIndex).setBackgroundColor(color);
于 2012-06-16T21:04:06.777 回答
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;
    }
}
}

你可以查看我的GithubYoutube教程

于 2018-11-27T07:50:27.830 回答