第42章、标签组件Tabhost(从零开始学Android)

  标签组件Tabhost类似于Windows应用中的选项框。

   TabHost的实现有两种方式,第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

  第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

 

一、继承TabActivity

  第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

  1、布局文件

  打开“res/layout/activity_main.xml”文件。

  输入以下代码:

<?xml version="1.0" encoding="utf-8"?>  

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" 
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
    
    <LinearLayout android:id="@+id/txtnews" 
        android:layout_width="match_parent"  
        android:layout_height="match_parent" 
        android:gravity="center_horizontal"  
        android:orientation="vertical">  
                              
       <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        
    </LinearLayout>  
    
    <LinearLayout 
        android:id="@+id/photonews" 
        android:layout_width="match_parent"  
        android:layout_height="match_parent" 
        android:gravity="center_horizontal"  
        android:orientation="vertical">  
        
        <AnalogClock android:id="@+id/clock"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" />  

    </LinearLayout>
      
    <LinearLayout android:id="@+id/videonews" 
        android:layout_width="match_parent"  
        android:layout_height="match_parent" 
        android:gravity="center_horizontal"  
        android:orientation="vertical">

        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToggleButton" />
        
    </LinearLayout>  

</FrameLayout>  


  2、程序文件 

  打开“src/com.genwoxue.switchtogglebutton/MainActivity.java”文件。

  然后输入以下代码:

package com.genwoxue.tabhost;

import android.app.TabActivity;   
import android.os.Bundle;   
import android.view.LayoutInflater;   
import android.widget.TabHost;  

public class MainActivity extends TabActivity {
	//声明TabHost
	private TabHost tabNews;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState); 
        //获得当前MainActivity(继承自TabActivity)的TabHost
        tabNews = this.getTabHost();  
        // 通俗的说,inflate就相当于将一个xml中定义的布局找出来 
        LayoutInflater.from(this).inflate(R.layout.activity_main,tabNews.getTabContentView(), true);  
        //添加一个文本新闻选项框
        tabNews.addTab(tabNews  
                 .newTabSpec("firsttab")  
                 .setIndicator("文本新闻",getResources().getDrawable(R.drawable.ic_launcher))  
                 .setContent(R.id.txtnews));
        //添加一个图片新闻选项框
        tabNews.addTab(tabNews  
                 .newTabSpec("secondtab")  
                 .setIndicator("图片新闻",getResources().getDrawable(R.drawable.ic_launcher))  
                 .setContent(R.id.photonews));  
        //添加一个视频新闻选项框
        tabNews.addTab(tabNews  
                 .newTabSpec("thirdtab")  
                 .setIndicator("视频新闻",getResources().getDrawable(R.drawable.ic_launcher))  
                 .setContent(R.id.videonews));  
    }  

}


  3、运行结果

     

 

 

 二、布局文件中定义TabHost

   第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

  1、布局文件

  打开“res/layout/activity_main.xml”文件。

  输入以下代码:

<?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="match_parent"    
    android:layout_height="match_parent">   
    <TabHost android:id="@+id/tabhost"  
         android:layout_width="fill_parent"  
         android:layout_height="wrap_content">  
         <LinearLayout  
            android:orientation="vertical"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent">  
              
             <TabWidget android:id="@android:id/tabs"   
              android:orientation="horizontal"  
              android:layout_width="match_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/text"  
                        android:layout_width="match_parent"  
                        android:layout_height="match_parent" 
                        android:text="第一个选项框"/>  
                   	 <TextView android:id="@+id/photo"  
                        android:layout_width="match_parent"  
                        android:layout_height="match_parent" 
                        android:text="第二个选项框"/>  
                  	  <TextView android:id="@+id/video"  
                        android:layout_width="match_parent"  
                        android:layout_height="match_parent" 
                        android:text="第三个选项框"/>  
             </FrameLayout>  
           
         </LinearLayout>  
    </TabHost>  
</LinearLayout>  


 

 

  2、程序文件 

  打开“src/com.genwoxue.tabhost/MainActivity.java”文件。

  然后输入以下代码:

package com.genwoxue.tabhost_b;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TabHost;
import android.widget.TabWidget;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        //获取TabHost对象
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
        tabHost.setup();  
        //获取TabWidget对象
        TabWidget tabWidget = tabHost.getTabWidget();
        //添加选项框一
        tabHost.addTab(tabHost  
                .newTabSpec("tab1")  
                .setIndicator("文本新闻",getResources().getDrawable(R.drawable.ic_launcher))  
                .setContent(R.id.text));  
        //添加选项框二
        tabHost.addTab(tabHost  
                .newTabSpec("tab2")  
                .setIndicator("图片新闻",getResources().getDrawable(R.drawable.ic_launcher))  
                .setContent(R.id.photo));  
        //添加选项框三
        tabHost.addTab(tabHost  
                .newTabSpec("tab3")  
                .setIndicator("视频新闻",getResources().getDrawable(R.drawable.ic_launcher))  
                .setContent(R.id.video));  

	}

}


  3、运行结果

      

 

补充说明:

  Android 关于inflate
  
  因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
  
  Viewview=View.inflate(this,R.layout.dialog_layout,null);
  
  TextViewdialogTV=(TextView)view.findViewById(R.id.dialog_tv);
  
  dialogTV.setText("abcd");
  
  如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
  
  三种方式可以生成LayoutInflater:
  
  LayoutInflaterinflater=LayoutInflater.from(this);
  
  LayoutInflaterinflater=getLayoutInflater();
  
  LayoutInflaterinflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
  
  然后调用inflate方法将xml布局文件转成View
  
  publicViewinflate(intresource,ViewGrouproot,booleanattachToRoot)
  
  在View类中,也有inflate方法
  
  publicstaticViewinflate(Contextcontext,intresource,ViewGrouproot)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒋会全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值