我很抱歉,如果已有答案,但我找不到任何可以帮助我的事情。
我创建了我的标签:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
String account_information;
TabLayout tabLayout;
Window window;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
//mViewPager.setOnTouchListener(View.OnTouchListener());
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setSelectedTabIndicatorColor(getColor(R.color.BLACK));
account_information = getIntent().getStringExtra("CURRENT_ACCOUNT");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//returning the current tabs
switch (position) {
case 0:
contactstab_main contactstab1 = new contactstab_main();
return contactstab1;
case 1:
dashboardtab_main dashboardtab2 = new dashboardtab_main();
return dashboardtab2;
case 2:
dropdowntab_main dropdowntab3 = new dropdowntab_main();
Bundle bundle = new Bundle();
bundle.putString("current_account", account_information);
dropdowntab3.setArguments(bundle);
return dropdowntab3;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "CONTACTS";
case 1:
return "DASHBOARD";
case 2:
return "PROFILE-DROPDOWN";
}
return null;
}
}
}
我发现有关更改选项卡颜色的任何信息仅适用于选定和未选定的选项卡,但我想拥有每个选项卡?Tabtitlebackground?使用不同的颜色,以便您可以看到3种颜色的标签栏,每个标签代表一个标签。
答案 0 :(得分:1)
这样做依赖于未记录的代码,因此它可能会破坏......但我怀疑它是否可能。
TabLayout
托管一个名为SlidingTabStrip
的私有类(从LinearLayout
扩展)。 SlidingTabStrip
的每个孩子都是TabView
,即代表单个标签的视图。因此,您可以编写这样的代码来设置各个选项卡的背景颜色:
ViewGroup tabStrip = (ViewGroup) tabLayout.getChildAt(0);
int[] colors = new int[] { 0xffccaaff, 0xffffccaa, 0xffaaffcc };
for (int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setBackgroundColor(colors[i]);
}