开发者

TabHost not filling entire screen when calculating tab width according to screen width

I have a TabHost containing five tabs within a horizontal ScrollView. I want all tabs to have the same size with a minimum size of 83dp. If the fifth tab fits on screen, all tabs are widened to fill the entire screen. Here is the code to achieve this:

int tabMinWidth = (int) (83 * density);
int tabWidth = tabMinWidth
int nrOfTabs = displayMetrics.widthPixels / tabMinWidth;
if (nrOfTabs > 4)
    tabWidth = displayMetrics.widthPixels / 5;
for(int i = 0; i < 5; i++)
    tabHost.getTabWidget().getChildAt(i).getLayoutParams().width = tabWidth;

Basically the code works fine. However, not the entire screen width is used by the tabs. Example: five tabs with 96px do not fill a 320x480 screen in landscape mode (about 20px unused space), although 96 x 5 is exactly 480. How can I improve the code so that th开发者_高级运维e tabs really fill the entire screen?

UPDATE: Solution: I found that resizing each tab by about 4-5% works quite well:

tabWidth = (int) (tabWidth *1.04);

UPDATE 2: I think I found the cause of the problem. The tabs have a negative right and left margin. Considering these margins solves the problem and the TabHost fills exactly the whole screen.


You could set the android:fillViewport="true" of the horizontal scrollbar.


Maybe using buttons will work.

When I use tabs, I normally just hide the tabwidget tag by setting android visibility as gone.

And add buttons to act as the tab buttons like

<?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">        
        <FrameLayout android:id="@android:id/tabcontent"  
            android:layout_width="fill_parent" android:layout_height="0dip" 
            android:layout_weight="1.0"/> 
        <FrameLayout android:layout_width="fill_parent"  
            android:layout_height="wrap_content"> 
            <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" 
                android:visibility="gone"/> 
            <LinearLayout android:layout_width="fill_parent"  
                android:layout_height="64dip"> 
                <Button android:layout_height="fill_parent" android:layout_width="0dip"  
                    android:layout_weight="1.0" 
                    android:background="@drawable/ic_tab_artists"  
                    android:id="@+id/artist_id" android:onClick="tabHandler"/> 
                <Button android:layout_height="fill_parent" android:layout_width="0dip"  
                    android:layout_weight="1.0" 
                    android:background="@drawable/ic_tab_artists"  
                    android:id="@+id/album_id" android:onClick="tabHandler"/>
            </LinearLayout>  
        </FrameLayout> 
    </LinearLayout> 
</TabHost> 

and I add a button click handler

public void tabHandler(View target){ 
    artistButton.setSelected(false); 
    albumButton.setSelected(false); 
    songButton.setSelected(false); 
    if(target.getId() == R.id.artist_id){ 
        tabHost.setCurrentTab(0); 
        artistButton.setSelected(true); 
    } else if(target.getId() == R.id.album_id){ 
        tabHost.setCurrentTab(1); 
        albumButton.setSelected(true); 
    }
} 

This makes it easier to customize tab buttons. You will need to try it out as im haven't really tried tabs in a horizontalscrollview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜