我创建了一个带有 TabHost 和 TabActivity 的 android 应用程序。在 Android 2.3.4 上,图标出现在我的选项卡上,但在 Android 4.2.2 上却没有。这是我的 TabHost 活动代码。所有的图标都在 res.drawable 文件夹中。
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
private static MainActivity instance = null;
/**
* This is the IO Singleton Instance
*
*/
public static MainActivity sharedInstance(){
if (instance == null){
instance = new MainActivity();
}
return instance;
}
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
//Tab for Main Menu
TabSpec menuSpec = tabHost.newTabSpec("Home");
menuSpec.setIndicator("Home", getResources().getDrawable(R.drawable.main_menu));
Intent menuIntent = new Intent(this, MenuActivity.class);
menuSpec.setContent(menuIntent);
//Tab for Evacuation Routes
TabSpec evacSpec = tabHost.newTabSpec("Evacuation Routes");
evacSpec.setIndicator("Evacuation Routes", getResources().getDrawable(R.drawable.icon29));
Intent evacIntent = new Intent(this, EvacRouteTableActivity.class);
evacSpec.setContent(evacIntent);
//Tab for Shelters
TabSpec shelterSpec = tabHost.newTabSpec("Shelter Routes");
shelterSpec.setIndicator("Shelter Routes", getResources().getDrawable(R.drawable.shelter));
Intent shelterIntent = new Intent(this, ShelterActivity.class);
shelterSpec.setContent(shelterIntent);
//Tab for Fueling Stations
TabSpec fuelSpec = tabHost.newTabSpec("Refueling Routes");
fuelSpec.setIndicator("Refueling Routes", getResources().getDrawable(R.drawable.fillingstation));
Intent fuelIntent = new Intent(this, FuelStopActivity.class);
fuelSpec.setContent(fuelIntent);
//Tab for the Map
TabSpec mapSpec = tabHost.newTabSpec("Map");
mapSpec.setIndicator("Map", getResources().getDrawable(R.drawable.fillingstation));
Intent mapIntent = new Intent(this, MapViewActivity.class);
mapSpec.setContent(mapIntent);
tabHost.addTab(menuSpec);
tabHost.addTab(evacSpec);
tabHost.addTab(shelterSpec);
tabHost.addTab(fuelSpec);
tabHost.addTab(mapSpec);
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
public void addTabs(TabHost tHost){
//Tab for the Map
TabSpec mapSpec = tHost.newTabSpec("Map");
mapSpec.setIndicator("Map", getResources().getDrawable(R.drawable.fillingstation));
Intent mapIntent = new Intent(this, MapViewActivity.class);
mapSpec.setContent(mapIntent);
tHost.addTab(mapSpec);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我的 layout.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"
android:padding="5dp">
<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"
android:padding="5dp"/>
</LinearLayout>
</TabHost>