开发者

How to set the width of an android tab in XML layout?

开发者 https://www.devze.com 2023-03-24 12:58 出处:网络
I\'ve seen this programmatic solution but am wonde开发者_JAVA技巧ring how do do this in the XML file.. my goal is to have a set of tabs up top the use all available screen width. I can\'t see how to d

I've seen this programmatic solution but am wonde开发者_JAVA技巧ring how do do this in the XML file.. my goal is to have a set of tabs up top the use all available screen width. I can't see how to do that currently. Setting layout_width for the tabhost to fill_parent does not do the trick.


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"/>
                <Button android:layout_height="fill_parent" android:layout_width="0dip"     
                    android:layout_weight="1.0"
                    android:background="@drawable/ic_tab_artists" 
                    android:id="@+id/song_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);
    } else if(target.getId() == R.id.song_id){
        tabHost.setCurrentTab(2);
        songButton.setSelected(true);
    }
}


I added a horizontal scrollbar and set android:fillViewport="true".

0

精彩评论

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