#2
kevin_012015-11-29 21:32
|
1.MainActivity.java
package com.example.tabhost_1;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class MainActivity extends TabActivity {
private TabHost th;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
th = getTabHost();//TabHost getTabHost() 这是TabActivity中的方法,返回一个TabHost
th.addTab(th.newTabSpec("小狗").setIndicator("tab1", getResources().getDrawable(R.drawable.p_12)));
th.addTab(th.newTabSpec("马尔大夫").setIndicator("tab2",getResources().getDrawable(R.drawable.p_9)));
th.addTab(th.newTabSpec("林志颖").setIndicator("tab3",getResources().getDrawable(R.drawable.p_10)));
setContentView(R.layout.tab);
th.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
if(tabId.equals("tab1")) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, DongWu.class);
startActivity(intent);
}
if(tabId.equals("tab1")) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, FengJin.class);
startActivity(intent);
}
if(tabId.equals("tab1")) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, RenWu.class);
startActivity(intent);
}
}
});
}
@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;
}
}
2.DongWu.java
package com.example.tabhost_1;
import android.app.Activity;
import android.os.Bundle;
public class DongWu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dongwu);
}
}
3.FengJin.java
package com.example.tabhost_1;
import android.app.Activity;
import android.os.Bundle;
public class FengJin extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fengjing);
}
}
4.RenWu.java
package com.example.tabhost_1;
import android.app.Activity;
import android.os.Bundle;
public class RenWu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.renwu);
}
}
1.activity_main.xml
<LinearLayout xmlns:android="http://schemas.
xmlns:tools="http://schemas.
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
2.tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- TabWidget组件id值不可变-->
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
<!-- FrameLayout布局,id值不可变-->
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs">
</FrameLayout>
</RelativeLayout>
</TabHost>
3.AndroidManifiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.
package="com.example.tabhost_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.tabhost_1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="DongWu"></activity>
<activity android:name="RenWu"></activity>
<activity android:name="FengJin"></activity>
</application>
</manifest>
结果在虚拟手机上一运行就自动退出了,不知道哪里出错了,还请高手帮一下忙