我添加了三个选项卡,使用它没有问题,如果你想让它可滚动,只需将 <HorizontalScrollView>
其作为父项添加到<TabWidget>
mainActivity.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="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@android:id/tabs" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
主要活动
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnTabChangeListener
{
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
TabSpec photospec = tabHost.newTabSpec("Home");
photospec.setIndicator("");
Intent photosIntent = new Intent(this, Download.class);
photospec.setContent(photosIntent);
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("");
Intent songsIntent = new Intent(this, Home.class);
songspec.setContent(songsIntent);
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("");
Intent videosIntent = new Intent(this, Album.class);
videospec.setContent(videosIntent);
tabHost.addTab(photospec);
tabHost.addTab(songspec);
tabHost.addTab(videospec);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_selected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 50;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 70;
tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 50;
tabHost.setCurrentTab(1);
tabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tab)
{
int index = tabHost.getCurrentTab();
if(index == 0)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_selected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_unselect);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);
}
else if(index == 1)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_selected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_unselect);
}
else if(index == 2)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.download_unselect);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.main_unselect);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.albums_selected);
}
}
}