尝试更改Android中Tabs的颜色

时间:2012-09-06 19:55:47

标签: android android-activity tabs android-tabhost

以下是我的Tab类。选中和取消选中时,我正在尝试更改选项卡的颜色。但是当我调用 tabHost.setOnTabChangedListener(MyOnTabChangeListener)时,应用程序崩溃了。我甚至无法使用该方法启动应用程序,它提供了nullpointerexception。我不知道该怎么办?有任何想法吗? /问候

@SuppressWarnings("deprecation")
public class Tabs extends TabActivity
{
private static final String TAG = "TabHostActivity";
private boolean mHaveShownStartDialog = false;
static TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_host);
    setOnCreatePreferences();

    try
    {

        addTab(getString(R.string.Search), R.drawable.searchtab, SearchTask.class );
        addTab(getString(R.string.Bookmarks), R.drawable.blackheart1, Bookmarks.class );
        addTab(getString(R.string.Latest), R.drawable.clock3, Latest.class );
        addTab(getString(R.string.QAndA), R.drawable.pen, LatestFeedback.class );

        getTabHost().setCurrentTab( 0 );
        tabHost.setOnTabChangedListener(MyOnTabChangeListener);


    }
    catch(Exception e)
    {
        Log.e(TAG, e.getMessage());
    }


}

public static OnTabChangeListener MyOnTabChangeListener = new OnTabChangeListener(){

    public void onTabChanged(String tabId) {
      for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
      {
          tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE);
      }

      tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.GRAY);
    }
};





//public static void setTabColor(TabHost tabhost) {
//    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
//    {
//        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
 //   }
 //   tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
//}


private void addTab( CharSequence label, int drawable_id, Class<?> c ) 
{
    TabHost.TabSpec spec = getTabHost().newTabSpec("tab" + " "+ label);

    spec.setIndicator( label, getResources().getDrawable( drawable_id ) );

    spec.setContent( new Intent().setClass( this, c ) );

    getTabHost().addTab( spec );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate( R.menu.tabs_menu, menu );
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch ( item.getItemId() ) 
    {
        case R.id.tabs_menu_options_item:
            //startActivityForResult( new Intent( this, Options.class ) , 0 ); 
            return true;

        default: return super.onOptionsItemSelected(item);
    }
}

private void setOnCreatePreferences()
{
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences( getBaseContext() );

    boolean mUseStartDialog = preferences.getBoolean( "use_dialog", true );
    if( mUseStartDialog ) 
    {
        if( !mHaveShownStartDialog )
        {
            mHaveShownStartDialog = true;
            startActivity( new Intent( this, WelcomeDialog.class ) );
        }
    }
}

}

2 个答案:

答案 0 :(得分:2)

我看不到你在哪里初始化tabHost变量。

答案 1 :(得分:0)

不要通过OnTabChangeListener执行此操作。每次用户按下选项卡时,您都不希望调用代码;)您可以在

之后直接编写代码
getTabHost().setCurrentTab( 0 );

这是我用于此场景的代码:

TabWidget widget = tabHost.getTabWidget();
int size = widget.getChildCount();
for (int i = 0; i < size; i++) {

    // Hail to the master of unreadable code!
    ((TextView)((ViewGroup)widget.getChildAt(i)).findViewById(android.R.id.title)).setTextColor(0xffffffff);
    widget.getChildAt(i).setBackgroundResource(R.drawable.tab_widget_background_selector);
}

我在此建议您应该使用选择器让平台为您处理所有事情。

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:state_selected="true"
    android:drawable="@drawable/tab_widget_background_selected"/>
  <item
    android:drawable="@drawable/tab_widget_background_casual"/>
  </selector>