Use of Android tahhost

Source: Internet
Author: User

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>

:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.