TabHost的简单实用

    xiaoxiao2022-06-30  53

    TabHost已经过时了的一个控件,如今维护以前的项目,自己重新复习下:

    (1)       设置布局:添加一个FragmentLayout作为内容的显示容器,添加TabWidget,作为选项卡控件

    (2)       Activity继承TabActivity

    (3)       TabSpc设置选项卡的内容,每个选项卡对应的内容,每个选项卡的标识。

     

     

    TabSpec page1 = tabhost.newTabSpec("one")//标识

                    .setIndicator("叫兽")//选项卡内容

                    .setContent(R.id.isanimal);//显示内容:可以设置View,和Intent,还有TabHost.TabContentFactory

    tabhost.addTab(page1);//添加选项卡和内容。

     

    布局:需要注意布局中的id

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        tools:context="com.example.mytabhost.MainActivity">

     

       <TabHost

            android:id="@android:id/tabhost"

            android:layout_width="match_parent"

            android:layout_height="match_parent" >

     

            <LinearLayout

               android:layout_width="match_parent"

               android:layout_height="match_parent"

               android:orientation="vertical">

     

                <FrameLayout

                   android:id="@android:id/tabcontent"

                   android:layout_width="match_parent"

                   android:layout_height="match_parent"

                   android:layout_weight="1">

                </FrameLayout>

     

                <TabWidget

                   android:id="@android:id/tabs"

                   android:layout_width="match_parent"

                   android:layout_height="wrap_content"

                   android:layout_weight="0"

                    android:background="#ff00ff00">

                </TabWidget>

            </LinearLayout>

       </TabHost>

     

    </LinearLayout>

    Activity:

    @SuppressWarnings("deprecation")

    public class MainActivity extendsTabActivity {

             TabHosttabhost;

             FrameLayouttab_content;

             IntentjiaoShouIntent,laoShiIntent,neZhaIntent;

       @Override

       protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           this.requestWindowFeature(Window.FEATURE_NO_TITLE);

           setContentView(R.layout.activity_main);

           tabhost=getTabHost();

           //tab_content=(FrameLayout) findViewById(R.id.tab_content);

           jiaoShouIntent=new Intent(this, JiaoShouActivity.class);

           laoShiIntent=new Intent(this, LaoShiActivity.class);

           neZhaIntent=new Intent(this, NeZhaActivity.class);

           setTab();

        }

       public void setTab(){

                 TabSpec page1 =tabhost.newTabSpec("one")

                    .setIndicator("叫兽")

                    .setContent(jiaoShouIntent);

                 tabhost.addTab(page1);//添加选项卡和内容。

            

            TabSpec page2 =tabhost.newTabSpec("two")

                    .setIndicator("老湿")

                    .setContent(laoShiIntent);

            tabhost.addTab(page2);

            

            TabSpec page3 =tabhost.newTabSpec("three")

                    .setIndicator("哪吒")

                    .setContent(neZhaIntent);

            tabhost.addTab(page3);

        }

    }

     

    与RadioGroup联合使用:

    布局:只是添加了一个RadioGroup

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        tools:context="com.example.mytabhost.MainActivity">

     

       <TabHost

            android:id="@android:id/tabhost"

            android:layout_width="match_parent"

            android:layout_height="match_parent" >

     

            <LinearLayout

               android:layout_width="match_parent"

               android:layout_height="match_parent"

               android:orientation="vertical">

     

                <FrameLayout

                   android:id="@android:id/tabcontent"

                   android:layout_width="match_parent"

                   android:layout_height="match_parent"

                   android:layout_weight="1">

                </FrameLayout>

     

                <TabWidget

                   android:id="@android:id/tabs"

                   android:layout_width="match_parent"

                   android:layout_height="wrap_content"

                   android:layout_weight="0">

     

                    <RadioGroup

                        android:layout_width="match_parent"

                        android:layout_height="wrap_content"

                        android:layout_weight="0"

                        android:orientation="horizontal">

     

                        <RadioButton

                            android:id="@+id/jiaoshou"

                            style="@style/main_tab_bottom"

                            android:drawableTop="@drawable/tabs_home"

                            android:text="教授" />

     

                        <RadioButton

                            android:id="@+id/laoshi"

                            style="@style/main_tab_bottom"

                            android:drawableTop="@drawable/tabs_sort"

                            android:text="老湿" />

     

                        <RadioButton

                            android:id="@+id/nezha"

                            style="@style/main_tab_bottom"

                          android:drawableTop="@drawable/tabs_search"

                            android:text="哪吒" />

                    </RadioGroup>

                </TabWidget>

            </LinearLayout>

       </TabHost>

     

    </LinearLayout>

    Activity:只是对RadioGroup中的Radiobutton设置监听,并设置选定的是某个Tabhost

    @SuppressWarnings("deprecation")

    public classMainActivity extends TabActivity implements OnCheckedChangeListener {

       TabHosttabhost;

       FrameLayouttab_content;

       IntentjiaoShouIntent,laoShiIntent,neZhaIntent;

       RadioButtonjiaoshou,laoshi,nezha;

       @Override

       protectedvoidonCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            this.requestWindowFeature(Window.FEATURE_NO_TITLE);

            setContentView(R.layout.activity_main);

            jiaoshou=(RadioButton) findViewById(R.id.jiaoshou);

            laoshi=(RadioButton) findViewById(R.id.laoshi);

            nezha=(RadioButton) findViewById(R.id.nezha);

            jiaoshou.setOnCheckedChangeListener(this);

            laoshi.setOnCheckedChangeListener(this);

            nezha.setOnCheckedChangeListener(this);

            tabhost=getTabHost();

            //tab_content=(FrameLayout) findViewById(R.id.tab_content);

            jiaoShouIntent=new Intent(this, JiaoShouActivity.class);

            laoShiIntent=new Intent(this, LaoShiActivity.class);

            neZhaIntent=new Intent(this, NeZhaActivity.class);

            setTab();

       }

       publicvoidsetTab(){

         TabSpec page1 = tabhost.newTabSpec("one")

                    .setIndicator("叫兽")

                    .setContent(jiaoShouIntent);

         tabhost.addTab(page1);//添加选项卡和内容。

            

            TabSpec page2 = tabhost.newTabSpec("two")

                    .setIndicator("老湿")

                    .setContent(laoShiIntent);

            tabhost.addTab(page2);

            

            TabSpec page3 = tabhost.newTabSpec("three")

                    .setIndicator("哪吒")

                    .setContent(neZhaIntent);

            tabhost.addTab(page3);

       }

       @Override

       public voidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {

          switch(buttonView.getId()){

          case R.id.jiaoshou:

            tabhost.setCurrentTabByTag("one");

            break;

          case R.id.laoshi:

            tabhost.setCurrentTabByTag("two");

            break;

          case R.id.nezha:

            tabhost.setCurrentTabByTag("three");

            break;

          }

       }

    }

     

    转载请注明原文地址: https://ju.6miu.com/read-1126055.html

    最新回复(0)