我想更改选项卡颜色和文本,当它被选中我已经尝试了很多在xml和android没有工作请帮助我扩展与片段和使用tabpageradapter继承我的代码
public class BusTab extends FragmentActivity implements OnTabChangeListener {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_tab);
TabAdapter = new TabPagerAdapter(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
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("BUSNO").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("ROUTE").setTabListener(tabListener));
}
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
将每个标签的tab_indicator用作
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@color/white"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_tab_txt"
style="?android:attr/tabWidgetStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/live"
android:textColor="@drawable/tab_text_color"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_gravity="bottom"
android:background="@drawable/tab_underline_selector" />
</LinearLayout>
<View
android:layout_width="1dp"
android:id="@+id/tabs_divider_view"
android:layout_height="30dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/grey_background" />
</LinearLayout>
和drawable tab_text_color为
<?xml version="1.0" encoding="utf-8"?>
<item android:state_selected="true" android:color="@color/dark_blue"/>
<item android:state_pressed="true" android:color="@color/dark_blue"/>
<item android:color="@color/dark_gray" />
和tab_underline_selector为
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="@color/dark_blue" android:state_selected="true"/>
<item android:drawable="@color/transparent"/>
并在你的片段中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.entries_tabs, container, false);
HorseRacesNowMainActivity.setHeaders(this);
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
View tabIndicatorToday = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorToday.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.today));
mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.today)).setIndicator(tabIndicatorToday), EntriesTodayFragment.class, null);
View tabIndicatorLive = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorLive.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.live));
mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.live)).setIndicator(tabIndicatorLive), EntriesLiveFragment.class, null);
View tabIndicatorTrack = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
((TextView) tabIndicatorTrack.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.by_track));
tabIndicatorTrack.findViewById(R.id.tabs_divider_view).setVisibility(View.INVISIBLE);
mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.by_track)).setIndicator(tabIndicatorTrack), EntriesTrackFragment.class, null);
return view;
}