我需要一些帮助来了解我缺少什么或做错了什么。我的目标是在选中时将标签的背景颜色设置为白色,在未选中时将其设置为黑色。但首先我只是想将背景颜色设置为红色,作为代码测试: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>