1

タブ付きアプリケーションの次のアクティビティコードがあります。

public class TabbedActivity extends TabActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabbedactivity);        

        TabHost tabHost = getTabHost();         

        TabSpec photospec = tabHost.newTabSpec("RP");
        // setting Title and Icon for the Tab
        photospec.setIndicator("RP", getResources().getDrawable(R.drawable.tabrp));
        Intent photosIntent = new Intent(this, RP.class);
        photospec.setContent(photosIntent);

        // Tab for Songs
        TabSpec songspec = tabHost.newTabSpec("MP");
        songspec.setIndicator("MP", getResources().getDrawable(R.drawable.tabmp));
        Intent songsIntent = new Intent(this, MP.class);
        songspec.setContent(songsIntent);

        tabHost.addTab(photospec);
        tabHost.addTab(songspec);           
    }       
}

tabbedactivity.xmlは次のように定義されています。

<?xml version="1.0" encoding="utf-8"?>
<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">
    <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>

次のようにtabmp.xmlとtabrp.xmlを追加しました。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/white"/>
    <item android:state_focused="true" android:drawable="@color/white"/>
    <item android:state_pressed="true" android:drawable="@color/white"/>
    <item android:drawable="@color/gray" />
</selector>

色はで定義されていcolor.xmlます。アプリケーションは実行されますが、タブの色は、アクティブな場合はデフォルトの黒、アクティブでない場合は灰色、押された場合は青のままです。これは、タブのデフォルトのAndroidの色です。tabmp.xmlとはtabrp.xml機能しないようです。私はここで何か間違ったことをしていますか?

4

1 に答える 1

1

描画リソースは、タブビューの背景として設定する必要があります。次の2つのいずれかを実行できます。

  1. タブを作成したら:

     tabHost.getTabWidget().getChildAt(THE_CHILDS_POSITION_IN_THE_HOST).setBackgroundResource(R.drawable.tabmp.xml);
    
  2. または、タブのカスタムレイアウトを作成します

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
    android:padding="10dp" android:gravity="center" android:orientation="vertical">
    
        <TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Title"
            android:textSize="15dp" android:textColor="@drawable/tab_text_selector"
            android:textStyle="bold" />
    </LinearLayout>
    

    次に、タブを作成するとき:

    View tabview = LayoutInflator.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView)view.findViewById(R.id.tabsText);
    tv.setText(TEXT);
    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(i);
    tabHost.addTab(setContent);
    
于 2012-07-13T22:49:11.767 に答える