iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android编程实现自定义Tab选项卡功能示例
  • 575
分享到

Android编程实现自定义Tab选项卡功能示例

示例tabAndroid 2022-06-06 04:06:19 575人浏览 独家记忆
摘要

本文实例讲述了Android编程实现自定义Tab选项卡功能。分享给大家供大家参考,具体如下: import android.app.TabActivity; import a

本文实例讲述了Android编程实现自定义Tab选项卡功能。分享给大家供大家参考,具体如下:


import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import android.widget.TabHost.OnTabChangeListener;
import android.os.Build;
import android.view.View;
import java.lang.reflect.Field;
import android.view.LayoutInflater;
public class testTabActivity extends TabActivity {
 
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     int width =45;
     int height =48;
     final TabHost tabs = getTabHost();
     final TabWidget tabWidget = tabs.getTabWidget();
     Field mBottomLeftStrip;
     Field mBottomRightStrip;
     LayoutInflater.from(this).inflate(R.layout.tab_views, tabs.getTabContentView(), true);
     tabs.addTab(tabs.newTabSpec("first tab")
       .setIndicator("信息",getResources().getDrawable(R.drawable.m))
       .setContent(new Intent(testTabActivity.this,OneActivty.class))
       );
     tabs.addTab(tabs.newTabSpec("second tab")
     .setIndicator("收藏",getResources().getDrawable(R.drawable.n))
     .setContent(R.id.content));
     tabs.addTab(tabs.newTabSpec("second tab")
       .setIndicator("设置",getResources().getDrawable(R.drawable.s))
       .setContent(R.id.content));
     for (int i =0; i < tabWidget.getChildCount(); i++) {
       
       tabWidget.getChildAt(i).getLayoutParams().height = height;
       tabWidget.getChildAt(i).getLayoutParams().width = width;
     
      final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
      tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
       
     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.no));
         mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.no));
        } catch (Exception e) {
         e.printStackTrace();
        }
     } else {
     
     }
     View vvv = tabWidget.getChildAt(i);
  if(tabs.getCurrentTab()==i){
      vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button));
  }
  else {
      vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar));
  }
     }
     
     tabs.setOnTabChangedListener(new OnTabChangeListener(){
  @Override
  public void onTabChanged(String tabId) {
   // TODO Auto-generated method stub
   for (int i =0; i < tabWidget.getChildCount(); i++) {
   View vvv = tabWidget.getChildAt(i);
   if(tabs.getCurrentTab()==i){
       vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button));
   }
   else {
       vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar));
   }
   }
  }});
   }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android操作sqlite数据库技巧总结》、《Android操作JSON格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:Android实现底部导航栏功能(选项卡)Android多个TAB选项卡切换效果Android仿微信底部实现Tab选项卡切换效果Android利用Fragment实现Tab选项卡效果Android实现类似网易新闻选项卡动态滑动效果Android编程实现将tab选项卡放在屏幕底部的方法Android开发之选项卡功能的实现方法示例Android实现知乎选项卡动态隐藏效果实例


--结束END--

本文标题: Android编程实现自定义Tab选项卡功能示例

本文链接: https://www.lsjlt.com/news/22305.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作