Frage

I'm trying to create custom tabs beneath the action bar. My action bar is already taken up by other widgets, so moving the tabs to the action bar is not an option. The tabs need to have both images and text. I'm having 2 problems:

1) When I do this:

mTabHost.addTab(mTabHost.newTabSpec("Local").setIndicator("Local", getResources().getDrawable(R.drawable.local_selector)).setContent(R.id.local_image));

I see the text and no image. If I do this:

mTabHost.addTab(mTabHost.newTabSpec("Local").setIndicator("", getResources().getDrawable(R.drawable.local_selector)).setContent(R.id.local_image));

I see the image, but I haven't included the desired text.

2) The background color of the text and image do not match the color of the TabWidget. It matches the default background color of the entire app.

I've also tried creating the tabs in a RelativeLayout and implementing them like this:

RelativeLayout localView = (RelativeLayout)inflater.inflate(R.layout.local_tab, null);
mTabHost.addTab(mTabHost.newTabSpec("Local").setIndicator(localView).setContent(R.id.local_image));

This gives me the desired image+text combo with the correct background color, but the tabs no longer look like tabs. There are no dividers and the selected tab doesn't light up.

War es hilfreich?

Lösung 2

I found the solution to my problem. In my theme, I was setting android:background instead of android:windowBackground. Once I used android:windowBackground, the colors worked together better.

Then I created new image with the text included. After lots of tweaking, I eventually got it to look the way I wanted.

Andere Tipps

try this may be it help you.

tabHost = getTabHost();
    TabSpec spec_info = tabHost.newTabSpec(getString(R.string.info));
    spec_info.setIndicator(getString(R.string.info), null);
    Intent Intent_info = new Intent(this, RestaurantInfoActivity.class);
    spec_info.setContent(Intent_info);
    tabHost.addTab(spec_info);
    tabHost.getTabWidget().getChildTabViewAt(0).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selector));

xml

<TabHost
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@android:id/tabhost"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:orientation="vertical" 
                    android:fadingEdge="none"
                    android:overScrollMode="never"
                    >

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:tabStripEnabled="false" />

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" />
                </LinearLayout>
            </TabHost>

selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use white-->
<item android:drawable="@drawable/active_tab"
      android:state_selected="true" />
<!-- When not selected, use grey -->
<item android:drawable="@drawable/in_active_tab" >
</item>

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top