我无法设置标签的背景颜色?

时间:2012-09-07 21:33:51

标签: android xml android-activity tabs

我需要一些帮助才能理解我所缺少或做错的事情。我的目标是在选中时将我的选项卡的背景颜色设置为白色,在未选中时将其设置为黑色。但首先,我只是尝试将背景颜色设置为红色,作为代码的测试: getTabHost()。getTabWidget()。getChildAt(0).setBackgroundColor(Color.parseColor(“#4E4E9C”) ); 即可。我的代码甚至在启动之前就崩溃了,原因如下:二进制XML文件行#19:你必须提供layout_width属性。我无能为力从哪里开始?我错过了什么?这与宽度属性有什么关系? /问候

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

    }

我的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>

1 个答案:

答案 0 :(得分:0)

R.drawable.searchtab中的资源,它是一个图像'.PNG'格式,如果是,它是否有足够的透明度..?

  

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