android布局学习(一):tabHost的应用

来源:互联网 发布:算法4 epub 编辑:程序博客网 时间:2024/05/16 07:20

         tabHost是我们在android开发中经常用到的控件之一。须熟练掌握。

1、先定义一个tabhost布局文件,里面的某些元素的Id是固定的,须注意:

<?xml version="1.0" encoding="utf-8" ?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="horizontal" >
    <!-- 注意Id -->
    <TabWidget 
        android:id="@android:id/tabs" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:visibility="gone" > 
    </TabWidget> 
    <!-- 注意Id --> 
 
    <FrameLayout 
        android:id="@android:id/tabcontent" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
    </FrameLayout> 
 
    <RadioGroup 
        android:id="@+id/rg_main_btns" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom" 
        android:background="#2BD0F5" 
        android:gravity="center_vertical" 
        android:orientation="horizontal" > 
 
        <!-- android:drawableTop="@drawable/at_icon" 这句的意思是在文字上面放入图片 --> 
 
        <RadioButton 
            android:id="@+id/first" 
            style="@style/main_style " 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:drawableTop="@drawable/group_add_checkbox_select" 
            android:text="第一个"
            android:background="@drawable/mainstyle" > 
        </RadioButton> 
 
        <RadioButton 
            android:id="@+id/second"
            
            style="@style/main_style " 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:drawableTop="@drawable/group_add_checkbox_select"
            android:background="@drawable/mainstyle"
            android:text="第二个" > 
        </RadioButton> 
 
        <RadioButton 
            android:id="@+id/third" 
            style="@style/main_style " 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:drawableTop="@drawable/group_add_checkbox_select" 
            android:text="第三个"
            android:background="@drawable/mainstyle" > 
        </RadioButton> 
 
        <RadioButton 
            android:id="@+id/four" 
            style="@style/main_style " 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:drawableTop="@drawable/group_add_checkbox_select" 
            android:text="第四个"
            android:background="@drawable/mainstyle" > 
        </RadioButton> 
    </RadioGroup>
</TabHost> 

 

2、在activity中的处理。

package cn.gsww.activity;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;

public class TabDemo2Activity extends TabActivity {
 
 
 private static final String FISRT = "first"; 
 private static final String SECOND = "second"; 
 private static final String THIRD = "third"; 
 private static final String FOUR = "four"; 
 private TabHost tabHost; 

 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        tabHost = this.getTabHost(); 
         // 点击相应选项选项,跳转到相应的Activity,创建TabSpec 
        TabSpec homeSpec = tabHost.newTabSpec(FISRT).setIndicator(FISRT).setContent(new Intent(this, FirstActivity.class)); 
        TabSpec atSpec = tabHost.newTabSpec(SECOND).setIndicator(SECOND).setContent(new Intent(this, SecondActivity.class)); 
        TabSpec msgSpec = tabHost.newTabSpec(THIRD).setIndicator(THIRD).setContent(new Intent(this, FirstActivity.class)); 
       TabSpec moreSpec = tabHost.newTabSpec(FOUR).setIndicator(FOUR).setContent(new Intent(this, SecondActivity.class)); 
     
        tabHost.addTab(homeSpec); 
        tabHost.addTab(atSpec); 
        tabHost.addTab(msgSpec); 
        tabHost.addTab(moreSpec);
        RadioGroup radioGroup = (RadioGroup) this.findViewById(R.id.rg_main_btns); 
         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
            /**
                     * 响应点击事件
                     */ 
                 public void onCheckedChanged(RadioGroup group, int checkedId) { 
     
                       switch (checkedId) { 
                        case R.id.first: 
                          tabHost.setCurrentTabByTag(FISRT); 
                          break; 
      
                     case R.id.second: 
                           tabHost.setCurrentTabByTag(SECOND); 
                           break; 
                      case R.id.third: 
                          tabHost.setCurrentTabByTag(THIRD); 
                            break; 
                       case R.id.four: 
                           tabHost.setCurrentTabByTag(FOUR); 
                          break; 
                       } 
                  } 
            }); 

    }
}

 

 注: 下面看下这样去使用还有一个体现Android性能的地方,当我们点击下面四个选项的时候,点击点击完成之后Activity将会被onCreate() 但是当点击一次之后,接下去再去点击,会发现不会去执行onCreate(),这样其实Activity只会被创建一次,这样效率高了很多: