Android tahhost is used. The tabactivity inheritance method is used here.
Here, three tabs are customized: American. XML, Chinese. XML, and Japanese. xml.
American. xml file
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="American1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="American2" /></LinearLayout>
Chinese. xml file
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chinese button 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chinese button 2" /></LinearLayout>
Japan. xml file
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Japanese button 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Japanese button 2" /></LinearLayout>
The java files corresponding to the three layout are americanactivity. Java, chinaactivity. Java, and Japan activity. java.
Americanactivity. Java File
public class AmericanActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.american); }}
Chinaactivity. Java File
public class ChinaActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chinese); }}
Japanactivity. Java File
public class JapanActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.japanese); }}
Mainactivity. JavaInherit tabactivity.
public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); Resources resources = getResources(); TabHost tabHost = getTabHost(); TabHost.TabSpec spec; Intent intent = new Intent(this,AmericanActivity.class ); spec = tabHost.newTabSpec("American"); spec.setIndicator("Ameican Tab"); spec.setContent(intent); tabHost.addTab(spec); Intent intent2 = new Intent(this,ChinaActivity.class ); spec = tabHost.newTabSpec("China"); spec.setIndicator("China Tab") spec.setContent(intent2); tabHost.addTab(spec); Intent intent3 = new Intent(this,JapanActivity.class ); spec = tabHost.newTabSpec("Japanese"); spec.setIndicator("Japanese Tab"); spec.setContent(intent3); tabHost.addTab(spec); tabHost.setCurrentTab(1); } }
Note: Add the following three lines of code to the androidmanifest file.
<activity android:name="com.example.app1.AmericanActivity" android:label="@string/app_name"></activity> <activity android:name="com.example.app1.JapanActivity" android:label="@string/app_name"></activity> <activity android:name="com.example.app1.ChinaActivity" android:label="@string/app_name"></activity>
: