简体   繁体   中英

I can't set the background-color of my tabs?

I need some help to understand what I am missing or doing wrong. My goal is to set the background-color of my tabs to White when being selected, and black when being unselected. But to start with I am just trying to set the background color to red, as a test with the code: getTabHost().getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#4E4E9C")); . My code below crashes the app even before it starts with the following reason: Binary XML file line #19: you must supply a layout_width attribute. I am clueless where to begin? What am I missing? What has this to do with a width attribute? /Regards

   public class Tabs extends TabActivity
   {
private static final String TAG = "TabHostActivity";
private boolean mHaveShownStartDialog = false;


@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 );
        getTabHost().getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#4E4E9C"));





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

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 initTabsAppearance(TabWidget tabWidget){
//  
//  for(int i=0; i<tabWidget.getChildCount(); i++)
//      tabWidget.getChildAt(i).setBackgroundResource(R.drawable.searchtab);
//  
//}

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 ) );
        }
      }
    }

    }

my tab_host.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</LinearLayout>
</TabHost>

The resource in R.drawable.searchtab, is it an image'.PNG' format,if it is, does it have enough transparency..?

addTab(getString(R.string.Search), R.drawable.searchtab , SearchTask.class );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM