TabWidgetTest

 
  <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" >
       <LinearLayout 
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" >
           <!-- 页面中最上面含n个tab的控件 -->
           <TabWidget 
               android:id="@android:id/tabs"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"/>
           <!--  -->
           <FrameLayout 
               android:id="@android:id/tabcontent"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent">
               <TextView 
                   android:id="@+id/textview1"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"
                   android:text="tab0tab0tab0"
                   android:background="#00aa00"/>
               <TextView 
                   android:id="@+id/textview2"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"
                   android:text="tab1tab1tab1"
                   android:background="#0000aa"/>
               <TextView 
                   android:id="@+id/textview3"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"
                   android:text="tab2tab2tab2"/>                                  
           </FrameLayout>          
       </LinearLayout>   
   </TabHost>

package hyz.com.tabwidget;

import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
//TabWidget类似于Android中查看电话簿的界面,通过多个标签切换显示不同的内容
public class TabWidgetTestActivity extends TabActivity implements OnTabChangeListener
{
	TabHost th;
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        //得到对象
         th = getTabHost(); 
        // LayoutInflater.from(this).inflate(R.layout.main, th.getTabContentView(), true); 可用此代替setContentView(R.layout.main);     
        th.addTab(th.newTabSpec("tab0").setIndicator("TAB1",getResources().getDrawable(R.drawable.img1)).setContent(R.id.textview1));
        th.addTab(th.newTabSpec("tab1").setIndicator("TAB2",getResources().getDrawable(R.drawable.img2)).setContent(R.id.textview2));
        th.addTab(th.newTabSpec("tab2").setIndicator("TAB3",getResources().getDrawable(R.drawable.img3)).setContent(R.id.textview2));
        //设置当前显示哪一个标签
        th.setCurrentTab(2);
        //此会覆盖布局TextView3的设置的背景颜色,其它两个不会
        th.setBackgroundColor(Color.argb(150, 150, 300, 20));
        //此会覆盖布局TextView3的上面设置中的背景颜色,其它两个不会
        th.setBackgroundResource(R.drawable.bg);
 
        //标签切换处理
        th.setOnTabChangedListener(this);
    }
    //tabId为newTabSpec("xxx")里面的字符串值
    public void onTabChanged(String tabId) 
	{
		Toast.makeText(this, "哈哈哈"+tabId, Toast.LENGTH_LONG).show();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值