android tab使用

android中使用tab选项卡。

1.继承TabActivity

2.布局文件中使用tabHost,tabWedgit和framework

3.在activity中通过源码添加tab选项卡,每个选项卡中显示指定activity中的内容。可以通过代码控制界面的显示效果。

 

public class MSN extends TabActivity{

public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   setContentView(R.layout.tab);//这里使用了上面创建的xml文件(Tab页面的布局)

   Resources res = getResources(); // Resource object to get Drawables

   final TabHost tabHost = getTabHost();  // The activity TabHost

   TabSpec spec;

   Intent intent;  // Reusable Intent for each tab

   final android.widget.TabWidget tabWidget = tabHost.getTabWidget();

   int height = 40;

   int width = 50;

 

 //第一个TAB

   intent = new Intent(this,MSNContactsList.class);//新建一个Intent用作Tab1显示的内容

   spec = tabHost.newTabSpec("好友列表")//新建一个 Tab

   .setIndicator("好友列表")//设置名称以及图标

   .setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx

   tabHost.addTab(spec);//添加进tabHost

 

 //第二个TAB

   intent = new Intent(this,MSNChatting.class);//第二个Intent用作Tab1显示的内容

   spec = tabHost.newTabSpec("正在聊天")//新建一个 Tab

   .setIndicator("正在聊天")//设置名称以及图标

   .setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx

   tabHost.addTab(spec);//添加进tabHost

 

tabHost.setCurrentTab(0); //设置当前的tab

 

for (int i =0; i < tabWidget.getChildCount(); i++) {

            //设置tab的高度、宽度

            tabWidget.getChildAt(i).getLayoutParams().height = height;

            tabWidget.getChildAt(i).getLayoutParams().width = width;

   }

 

 //当点击tab选项卡的时候,更改当前的背景

   tabHost.setOnTabChangedListener(new OnTabChangeListener(){

  @Override

  public void onTabChanged(String tabId) {

   // TODO Auto-generated method stub

   for (int i =0; i < tabWidget.getChildCount(); i++) {

    View v = tabWidget.getChildAt(i);

    if(tabHost.getCurrentTab()==i){

            v.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_lessblue));

            //更改tab标题颜色

            final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);

            tv.setTextColor(getResources().getColorStateList(android.R.color.darker_gray));

    }

    else {

        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_blue));

        //更改tab标题颜色

            final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);

            tv.setTextColor(getResources().getColorStateList(android.R.color.white));

    }

   }

  }});

 

 /**

         * 此方法是为了去掉系统默认的白色的底角

         * 

         * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip

         * 都是私有变量,但是我们可以通过反射来获取

         * 

         * 由于还不知道Android 2.2的接口是怎么样的,现在先加个判断好一些

         */

   Field mBottomLeftStrip; 

        Field mBottomRightStrip; 

 

     if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) {

           try { 

              mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip"); 

              mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip"); 

 

              if(!mBottomLeftStrip.isAccessible()) { 

                mBottomLeftStrip.setAccessible(true); 

              } 

 

              if(!mBottomRightStrip.isAccessible()){ 

                mBottomRightStrip.setAccessible(true); 

              } 

 

             mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.tab_border1)); 

             mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.tab_border1));         

           } catch (Exception e) { 

             e.printStackTrace(); 

           } 

     } 

}

}

 

布局文件tab.xml:

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

<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"

android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

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

android:layout_width="fill_parent" android:layout_height="wrap_content"></TabWidget>

<FrameLayout

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

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            android:padding="5dp" />

</LinearLayout>

</TabHost>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值