Basic knowledge about Android 27: tabhost01-simple application

Source: Internet
Author: User

The tabhost tab is often used in Android applications. Here, we will show you how to use the tabhost tab in the apidemo example ,,,
There are three common methods:
1. Create each tab page from a layout ID

public class Tabs1 extends TabActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TabHost tabHost = getTabHost();                LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("tab1")                .setContent(R.id.view1));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab2")                .setContent(R.id.view2));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab3")                .setContent(R.id.view3));    }}

2. dynamically create a view as the tab page content

public class Tabs2 extends TabActivity implements TabHost.TabContentFactory {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final TabHost tabHost = getTabHost();        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on))                .setContent(this));        tabHost.addTab(tabHost.newTabSpec("tab2")                .setIndicator("tab2")                .setContent(this));        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("tab3")                .setContent(this));    }    /** {@inheritDoc} */    public View createTabContent(String tag) {        final TextView tv = new TextView(this);        tv.setText("Content for tab with tag " + tag);        return tv;    }}

3. Pass an intent to pass the parameter, and use the newly started activity as the tab content. This should be easier to pass the parameter...

public class Tabs3 extends TabActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        final TabHost tabHost = getTabHost();        tabHost.addTab(tabHost.newTabSpec("tab1")                .setIndicator("list")                .setContent(new Intent(this, List1.class)));        tabHost.addTab(tabHost.newTabSpec("tab2")                .setIndicator("photo list")                .setContent(new Intent(this, List8.class)));                // This tab sets the intent flag so that it is recreated each time        // the tab is clicked.        tabHost.addTab(tabHost.newTabSpec("tab3")                .setIndicator("destroy")                .setContent(new Intent(this, Controls2.class)                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));    }}

There are two ways to implement tabhost. The first method inherits tabactivity and obtains tabhost from tabactivity using the gettabhost () method. The content of each tab can be defined in the layout file.

Mainactivity. xml

Private tabhost mytabhost; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // setcontentview (R. layout. main); mytabhost = This. gettabhost (); layoutinflater. from (this ). inflate (R. layout. main, mytabhost. gettabcontentview (), true); mytabhost. addtab (mytabhost. newtabspec ("Tab 1 "). setindicator ("Tab 1", getresources (). getdrawable (R. drawable. img01 )). setcontent (R. id. ll01); mytabhost. addtab (mytabhost. newtabspec ("Tab 2 "). setindicator ("Tab 2", getresources (). getdrawable (R. drawable. img02 )). setcontent (R. id. ll01); mytabhost. addtab (mytabhost. newtabspec ("tab 3 "). setindicator ("tab 3", getresources (). getdrawable (R. drawable. img03 )). setcontent (R. id. ll03 ));}

Tab content layout file:

<?xml version="1.0" encoding="utf-8"?>  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical" android:layout_width="fill_parent"      android:layout_height="fill_parent">      <LinearLayout android:id="@+id/ll01" android:layout_width="fill_parent"          android:layout_height="fill_parent" android:gravity="center_horizontal"          android:orientation="vertical">          <EditText android:id="@+id/widget34" android:layout_width="fill_parent"              android:layout_height="wrap_content" android:text="EditText"              android:textSize="18sp">          </EditText>          <Button android:id="@+id/widget30" android:layout_width="wrap_content"              android:layout_height="wrap_content" android:text="Button">          </Button>      </LinearLayout>      <LinearLayout android:id="@+id/ll02" android:layout_width="fill_parent"          android:layout_height="fill_parent" android:gravity="center_horizontal"          android:orientation="vertical">          <AnalogClock android:id="@+id/widget36"              android:layout_width="wrap_content" android:layout_height="wrap_content">          </AnalogClock>      </LinearLayout>      <LinearLayout android:id="@+id/ll03" android:layout_width="fill_parent"          android:layout_height="fill_parent" android:gravity="center_horizontal"          android:orientation="vertical">          <RadioGroup android:id="@+id/widget43"              android:layout_width="166px" android:layout_height="98px"              android:orientation="vertical">              <RadioButton android:id="@+id/widget44"                  android:layout_width="wrap_content" android:layout_height="wrap_content"                  android:text="RadioButton">              </RadioButton>              <RadioButton android:id="@+id/widget45"                  android:layout_width="wrap_content" android:layout_height="wrap_content"                  android:text="RadioButton">              </RadioButton>          </RadioGroup>      </LinearLayout>  </FrameLayout>  

The second method does not inherit tabactivity. You can define tabhost in the layout file, but the ID of tabwidget must be @ Android: ID/tabs. the ID of framelayout must be @ Android: ID/tabcontent.

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/hometabs"      android:orientation="vertical"      android:layout_width="fill_parent"        android:layout_height="fill_parent">       <TabHost android:id="@+id/tabhost"           android:layout_width="fill_parent"           android:layout_height="wrap_content">           <LinearLayout              android:orientation="vertical"              android:layout_width="fill_parent"              android:layout_height="fill_parent">                             <TabWidget android:id="@android:id/tabs"                 android:orientation="horizontal"                android:layout_width="fill_parent"                android:layout_height="wrap_content">              </TabWidget>                          <FrameLayout android:id="@android:id/tabcontent"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content">                        <TextView android:id="@+id/view1"                          android:layout_width="fill_parent"                          android:layout_height="fill_parent" android:text="Tab1"/>                      <TextView android:id="@+id/view2"                          android:layout_width="fill_parent"                          android:layout_height="fill_parent" android:text="Tab2"/>                      <TextView android:id="@+id/view3"                          android:layout_width="fill_parent"                          android:layout_height="fill_parent" android:text="Tab3"/>               </FrameLayout>                      </LinearLayout>      </TabHost>  </LinearLayout>  

Mainactivity

@Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          TabHost tabHost = (TabHost) findViewById(R.id.tabhost);          tabHost.setup();          TabWidget tabWidget = tabHost.getTabWidget();          tabHost.addTab(tabHost                  .newTabSpec("tab1")                  .setIndicator("tab1",                          getResources().getDrawable(R.drawable.img01))                  .setContent(R.id.view1));          tabHost.addTab(tabHost                  .newTabSpec("tab2")                  .setIndicator("tab2",                          getResources().getDrawable(R.drawable.img02))                  .setContent(R.id.view2));          tabHost.addTab(tabHost                  .newTabSpec("tab3")                  .setIndicator("tab3",                          getResources().getDrawable(R.drawable.img03))                  .setContent(R.id.view3));  

Instance code: tab.rar

References:

Android learning notes-tabhost tab usage

Tabhost implementation methods

Tab and tabhost

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.