[英]I can't set the background-color of my tabs?
我需要一些幫助來了解我的缺失或做錯了什么。 我的目標是在選中時將選項卡的背景色設置為白色,而在未選中時將其設置為黑色。 但首先,我只是嘗試將背景色設置為紅色,以進行代碼測試: getTabHost()。getTabWidget()。getChildAt(0).setBackgroundColor(Color.parseColor(“#4E4E9C”))); 。 我的以下代碼甚至在應用程序啟動前由於以下原因使應用程序崩潰:二進制XML文件第19行:您必須提供layout_width屬性。 我不知道從哪里開始? 我想念什么? 這與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>
R.drawable.searchtab中的資源是否為圖像“ .PNG”格式(如果是),是否具有足夠的透明度?
addTab(getString(R.string.Search), R.drawable.searchtab ,SearchTask.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.