问题
I want to change my ActionBar's Tabs background color with tabs selector line at bottom color.
I want to do that by using java code not xml.
I have tried creating ActionBar tabs ..
actionBar = getActionBar();
// Hide the action bar title
ActionBar actionBar.setDisplayShowTitleEnabled(false);
// Enabling Spinner dropdown navigation
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab PlayerTab = actionBar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionBar.newTab().setText("Fragment B");
//create the two fragments we want to use for display content
//////////////////////// Fragment PlayerFragment = new AFragment();
/////////////////// Fragment StationsFragment = new BFragment();
//set the Tab listener. Now we can listen for clicks.
///////////////////PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
///////////////// ////StationsTab.setTabListener(new MyTabsListener(StationsFragment));
//add the two tabs to the actionbar
actionBar.addTab(PlayerTab);
actionBar.addTab(StationsTab);
Now when I try to set background color with tabs line selector color, I get the error Java.lang.NullPointException
My OnTabSelcted() method ..
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
//tab.setCustomView(getResources().getDrawable(R.drawable.tabs_selector_blue));
System.out.println("Tab position is " +tab.getPosition());
try{
if(tab.getCustomView() == null){
tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
tab.setCustomView(tabLayout);
}else{
Toast.makeText(getApplicationContext(), "check for tabs", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
e.printStackTrace();
}
}
I have defined a custom selector for background and need to inflate it.
I am getting on line tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
Please let show me where's my error.
回答1:
I see you are using my solution. Your tabLayout may be null if you didn't initialise it when creating tabs. Here is the code I used to create tabs first, before changing background and text color in onTabSelected
for (int i = 0; i < TABS.length; i++) {
Tab tab = actionBar.newTab();
tab.setText(TABS[i]);
tab.setTabListener(this);
tab.setCustomView(getTabView(i)); << see getTabView() method below
actionBar.addTab(tab, i, (currentTab == i));
}
getTabView() method :
private View getTabView(int i) {
// TODO Auto-generated method stub
RelativeLayout r = (RelativeLayout) getLayoutInflater().inflate(
R.layout.custom_tab, null);
TextView t = (TextView) r.findViewById(R.id.custom_tab_text);
t.setText(TABS[i]);
int color;
switch (i) {
case MyObject.TYPE_A:
color = MyObject.COLOR_A;
break;
case MyObject.TYPE_B:
color = MyObject.B;
break;
case MyObject.TYPE_C:
color = MyObject.COLOR_C;
break;
case MyObject.TYPE_E:
color = MyObject.COLOR_E;
break;
case MyObject.TYPE_F:
color = MyObject.COLOR_F;
break;
default:
color = MyObject.COLOR_A;
break;
}
t.setTextColor(color);
return r;
}
Finally, custom_tab.xml, the layout of tabs:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_margin="0dp">
<TextView
android:id="@+id/custom_tab_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:gravity="center|center_horizontal"
android:textStyle="bold"/>
</RelativeLayout>
回答2:
see the statement
RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
and if condition
if(tab.getCustomView() == null){
tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
tab.setCustomView(tabLayout);
}
Now, if tab.getCustomView() returns null, then tabLayout is also null and the if condition becomes true. and inside if condition, you are referencing tabLayout which is null...
来源:https://stackoverflow.com/questions/21673531/programmatically-set-actionbartab-background-color-with-tab-selector-line-color