TabHost详解

来源:互联网 发布:唐山军工电脑保密软件 编辑:程序博客网 时间:2024/05/29 14:50

此文转载出处:http://blog.csdn.net/harvic880925/article/details/17120325

方法一、定义tabhost:不用继承TabActivity

1、布局文件:activity_main.xml

<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=".MainActivity" >      <Button          android:id="@+id/button1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Button" />       <TabHost          android:id="@+id/tabhost"          android:layout_width="match_parent"          android:layout_height="wrap_content">            <LinearLayout              android:layout_width="match_parent"              android:layout_height="match_parent"              android:orientation="vertical" >                <TabWidget                  android:id="@android:id/tabs"                  android:layout_width="match_parent"                  android:layout_height="wrap_content" >              </TabWidget>                <FrameLayout                  android:id="@android:id/tabcontent"                  android:layout_width="match_parent"                  android:layout_height="match_parent" >                    <!-- 第一个tab的布局 -->                  <LinearLayout                      android:id="@+id/tab1"                      android:layout_width="match_parent"                      android:layout_height="match_parent" >                        <TextView                          android:id="@+id/textView1"                          android:layout_width="wrap_content"                          android:layout_height="wrap_content"                          android:text="林炳东" />                    </LinearLayout>                    <!-- 第二个tab的布局 -->                  <LinearLayout                      android:id="@+id/tab2"                      android:layout_width="match_parent"                      android:layout_height="match_parent" >                        <TextView                          android:id="@+id/textView2"                          android:layout_width="wrap_content"                          android:layout_height="wrap_content"                          android:text="张小媛" />                    </LinearLayout>                    <!-- 第三个tab的布局 -->                  <LinearLayout                      android:id="@+id/tab3"                      android:layout_width="match_parent"                      android:layout_height="match_parent" >                        <TextView                          android:id="@+id/textView3"                          android:layout_width="wrap_content"                          android:layout_height="wrap_content"                          android:text="马贝贝" />                    </LinearLayout>              </FrameLayout>          </LinearLayout>      </TabHost>         </LinearLayout>  

2、JAVA代码

public class MainActivity extends Activity {        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);                    TabHost th=(TabHost)findViewById(R.id.tabhost);          th.setup();            //初始化TabHost容器                    //在TabHost创建标签,然后设置:标题/图标/标签页布局          th.addTab(th.newTabSpec("tab1").setIndicator("标签1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));          th.addTab(th.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2));          th.addTab(th.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3));               //上面的null可以为getResources().getDrawable(R.drawable.图片名)设置图标         }  }  

方法二:Tab的内容分开:不用继承TabActivity

 1、第一个tab的XML布局文件,tab1.xml:

<?xml version="1.0" encoding="UTF-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"         android:id="@+id/LinearLayout01"          android:layout_width="wrap_content"         android:layout_height="wrap_content">         <TextView               android:text="我是标签1的内容喔"              android:id="@+id/TextView01"               android:layout_width="wrap_content"              android:layout_height="wrap_content">         </TextView>   </LinearLayout>  

2、第二个tab的XML布局文件,tab2.xml: 

<?xml version="1.0" encoding="UTF-8"?>  <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"          android:id="@+id/LinearLayout02"          android:layout_width="wrap_content"          android:layout_height="wrap_content">            <TextView android:text="标签2"                    android:id="@+id/TextView01"                     android:layout_width="wrap_content"                    android:layout_height="wrap_content" />  </LinearLayout>  


0 0
原创粉丝点击