2011-07-20 6 views
1

私はタブホストを作成しました。私がしたいのは、選択した各タブホストの背景色を変更することです。私の場合、3つのタブホストを使用しています。最初のタブの背景色は白と残りの2つは黒です...今私が欲しいのは、私が2番目のタブをクリックすると背景色が白に変わり、残り2つが黒になるなどです。私は自分のコードを送信しています。これを行うには問題がタブホスト

public void createTabHost() 
     { 
        //create tabs 
        Resources res = getResources(); 
        TabHost MainTabHost = getTabHost(); 
        TabHost.TabSpec spec; 
        Intent intent; 

        if(7 < Build.VERSION.SDK_INT) 
        { 
         MainTabHost.getTabWidget().setStripEnabled(false); 
        } 

        //call calendar Activity class 
        intent = new Intent().setClass(this, CalendarForm.class); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        spec = MainTabHost.newTabSpec("Calendar").setIndicator("Calendar", 
        res.getDrawable(R.drawable.calendar_ic)).setContent(intent); 
        MainTabHost.addTab(spec); 

        //call History Activity class 
        intent = new Intent().setClass(this, HistoryForm.class); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        spec = MainTabHost.newTabSpec("History").setIndicator("History", 
        res.getDrawable(R.drawable.calendar_ic)).setContent(intent); 
        MainTabHost.addTab(spec); 

        //call Statistic Activity class 
        intent = new Intent().setClass(this, StatisticForm.class); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        spec = MainTabHost.newTabSpec("Statistic").setIndicator("Statistic", 
        res.getDrawable(R.drawable.calendar_ic)).setContent(intent); 
        MainTabHost.addTab(spec); 

        //setbackground Style of tabHost 
        MainTabHost.setCurrentTab(0); 
        MainTabHost.getTabWidget().setWeightSum(3); 
        TabWidget tabHost=getTabWidget(); 
        MainTabHost.setBackgroundColor(Color.parseColor("#E0DCC9")); 
        //MainTabHost.setBackgroundResource(R.drawable.back_img); 

        for (int j = 0; j < MainTabHost.getTabWidget().getChildCount(); j++) 
        { 
         ((TextView)tabHost.getChildAt(j).findViewById(android.R.id.title)).setTextColor(Color.parseColor("#FFFFFF")); 
         MainTabHost.getTabWidget().getChildAt(j).setBackgroundColor(Color.parseColor("#000000")); 
        } 
        MainTabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#E0DCC9")); 
        ((TextView)tabHost.getChildAt(0).findViewById(android.R.id.title)).setTextColor(Color.parseColor("#000000")); 


     } 

答えて

2

xmlレイアウトファイルでこれを実行してください。

値:android:background="@drawable/mytabhostbgを使用して、新しいxmlファイル名mytabhostbgを、drawableフォルダに作成します。

それは次のようになります。

< ?xml version="1.0" encoding="utf-8"?> < selector xmlns:android="http://schemas.android.com/apk/res/android"> < item android:state_window_focused="false" android:drawable="@drawable/buttonbackbleu" < item android:state_pressed="true" android:drawable="@drawable/buttonbackjaune" /> < item android:state_focused="true" android:drawable="@drawable/buttonbackbleu" /> < item android:state_enabled="true" android:drawable="@drawable/buttonbackbleu" /> </selector>

enter image description here

<?xml version="1.0" encoding="UTF-8"?> 

<item android:drawable="@drawable/back_image" 
     android:state_selected="true" 
     android:state_pressed="false" /> 
<!-- When not selected, use white--> 
<item android:drawable="@drawable/hq_ii" /> 

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#2C6B4C"> 
    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="0dp"> 
    <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" 
    android:padding="0dp" 
    /> 
    </LinearLayout> 
    </TabHost> 
+0

私はそれを試しましたが、全体のタブをカバーしていません...それはバックグラウンドを設定することを意味しますが、タブの半分のみです..スナップショットを送信します。 – AndroidDev

+0

私はあなたにスナップショットを送信しました..jsutはそれを確認します – AndroidDev

+0

何かをチェックするために5分;) – Atheh