简体   繁体   English

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

[英]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")); 但首先,我只是尝试将背景色设置为红色,以进行代码测试: 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. 我的以下代码甚至在应用程序启动前由于以下原因使应用程序崩溃:二进制XML文件第19行:您必须提供layout_width属性。 I am clueless where to begin? 我不知道从哪里开始? What am I missing? 我想念什么? What has this to do with a width attribute? 这与width属性有什么关系? /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 我的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..? R.drawable.searchtab中的资源是否为图像“ .PNG”格式(如果是),是否具有足够的透明度?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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