本文介绍了分别为每个操作栏选项卡添加不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要为每个标签添加不同的颜色.
I need to add a different color for every tabs.
例如:如下图所示
MainActivity.java:
// Add New Tab
actionBar.addTab(actionBar.newTab().setText("Home")
.setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("News")
.setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Latest")
.setTabListener(tabListener));
Home.java:
public class Home extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater
.inflate(R.layout.fragment_home, container, false);
((TextView) v.findViewById(R.id.textView)).setText("Home");
return v;
}
}
styles.xml:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
现在我创建了三个标签.现在我需要为每个标签分别添加不同的颜色.我需要一些关于此的建议.谢谢.
Right now I am created the three tabs.Now I need to add a different color for each tab separately.I need some suggestion regarding to this.Thank you.
推荐答案
是的,我终于做到了.
MainActivity.java:
public class MainActivity extends FragmentActivity {
static ViewPager Tab;
TabsPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabsPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
// Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ActionBar.Tab tab = actionBar.newTab().setText("Home")
.setTabListener(new TabListener(this, Home.class.getName()));
View tabView = inflater.inflate(R.layout.fragment_home, null);
tabView.setBackgroundResource(R.drawable.gradient_shape); // set custom
// color
tab.setCustomView(tabView);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("News")
.setTabListener(new TabListener(this, News.class.getName()));
View tabView2 = inflater.inflate(R.layout.fragment_news, null);
tabView2.setBackgroundResource(R.drawable.gradient_shape2); // set
// custom
// color
tab.setCustomView(tabView2);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("Latest")
.setTabListener(new TabListener(this, Latest.class.getName()));
View tabView3 = inflater.inflate(R.layout.fragment_latest, null);
tabView3.setBackgroundResource(R.drawable.gradient_shape3); // set
// custom
// color
tab.setCustomView(tabView3);
actionBar.addTab(tab);
}
public static class TabListener extends Fragment implements
ActionBar.TabListener {
public TabListener(MainActivity mainActivity, String name) {
// this(mainActivity,name);
}
@Override
public void onTabSelected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
}
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
}
}
}
gradient_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="4dp" />
<stroke
android:width="1dp"
android:color="#0078a5" />
<gradient
android:angle="90"
android:endColor="#00adee"
android:startColor="#0078a5" />
<padding
android:bottom="25dp"
android:left="50dp"
android:right="50dp"
android:top="25dp" />
</shape>
输出:
希望对您有所帮助.
这篇关于分别为每个操作栏选项卡添加不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!