Android之Tab类总结
本文主要包括以下Tab类实现方式
- FragmentTabHost+Fragment实现
- 传统的ViewPager实现
- FragmentManager+Fragment实现
- ViewPager+FragmentPagerAdapter实现
- TabPageIndicator+ViewPager+FragmentPagerAdapter
FragmentTabHost+Fragment实现
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout
android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical" ><FrameLayout
android:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" ></FrameLayout><TabWidget
android:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="60dip" /></LinearLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>
MainActivity
package com.darna.wmxfx;import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;import com.darna.wmxfx.fragment.Frg_Cart;
import com.darna.wmxfx.fragment.Frg_Index;
import com.darna.wmxfx.fragment.Frg_UserCenter;public class MainActivity extends FragmentActivity {private FragmentTabHost mTabHost = null;private int iTabIndex = 0;private int[][] iTab = new int[][] { { R.drawable.indexunused, R.drawable.cartunused, R.drawable.usercenterunused },{ R.drawable.indexused, R.drawable.cartused, R.drawable.usercenterused } };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();}private void initViews(){String[] strTab = getResources().getStringArray(R.array.bottom_tab);RelativeLayout[] relativeLayout = new RelativeLayout[strTab.length];for(int i = 0; i < strTab.length; i++){if (i == 0) {relativeLayout[i] = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_bottom, null);relativeLayout[i].findViewById(R.id.iv_bottom_tab).setBackgroundResource(iTab[1][i]);TextView tView = (TextView) relativeLayout[i].findViewById(R.id.tv_bottom_tab);tView.setText(strTab[i]);tView.setTextColor(getResources().getColor(R.color.textused));}else {relativeLayout[i] = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_bottom, null);relativeLayout[i].findViewById(R.id.iv_bottom_tab).setBackgroundResource(iTab[0][i]);TextView t = (TextView) relativeLayout[i].findViewById(R.id.tv_bottom_tab);t.setText(strTab[i]);t.setTextColor(getResources().getColor(R.color.textunused));}}mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_INDEX) .setIndicator(relativeLayout[0]), Frg_Index.class, null); mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_CART) .setIndicator(relativeLayout[1]), Frg_Cart.class, null);mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_USERCENTER) .setIndicator(relativeLayout[2]), Frg_UserCenter.class, null);mTabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {// TODO Auto-generated method stubView v = mTabHost.getTabWidget().getChildAt(iTabIndex);((ImageView) v.findViewById(R.id.iv_bottom_tab)).setBackgroundResource(iTab[0][iTabIndex]);((TextView) v.findViewById(R.id.tv_bottom_tab)).setTextColor(getResources().getColor(R.color.textunused));v = mTabHost.getCurrentTabView();iTabIndex = mTabHost.getCurrentTab();((ImageView) v.findViewById(R.id.iv_bottom_tab)).setBackgroundResource(iTab[1][iTabIndex]);((TextView) v.findViewById(R.id.tv_bottom_tab)).setTextColor(getResources().getColor(R.color.textused));}});}
}
效果如下
传统的ViewPager实现
FragmentManager+Fragment实现
ViewPager+Fragment实现
TabPageIndicator+ViewPager+FragmentPagerAdapter
实现方式和3是一致的,但是使用了TabPageIndicator作为tab的指示器,效果还是不错的,这个之前写过,就不再贴代码了。
效果图:
后四种方式详细可以参见
Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager - Hongyang - 博客频道 - CSDN.NET
完成
Android之Tab类总结相关推荐
- Android的Window类详解
Android的Window类(一) Android的GUI层并不复杂.它的复杂度类似于WGUI这类基于布局和对话框的GUI,与MFC.Qt等大型框架没有可比性,甚至飞漫魏永明的MiniGUI都比它复 ...
- android新闻管理,Android资讯新闻类App(头条、网易等),频道管理集成之数据库存储...
Android资讯新闻类App(头条.网易等),频道管理集成之数据库存储 发布时间:2019-01-16 19:55, 浏览次数:305 , 标签: Android App * 经常看新闻类的大家都知 ...
- android 实现自定义监听接口,Android在自定义类中实现自定义监听器方式
Android在自定义类中实现自定义监听器方式 发布时间:2020-08-31 06:19:39 来源:脚本之家 阅读:203 作者:Simon_Qi 监听器可以说是Android开发中最常用的东西之 ...
- Android Studio新建类头部注释和添加函数注释模板及快捷键
一,Android Studio新建类头部注释 是不是有时候看到这个很心烦 其实Studio中有设置修改这些注释模板的信息的功能 其实很简单,只需要两步: 1.打开Setting设置面板,找到File ...
- android sharedpreferences工具类
今天,简单讲讲如何写一个sharedpreferences的工具类. 很简单,把一些重复的操作封装在工具类里,其他地方调用就可以.在网上搜索了比较多的资料,找到一个比较好的工具类. 参考文章:http ...
- Android中Cursor类的概念和用法
使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android 中使用 Cursor 的理解. 关于 Cursor 在你理解和使用 Android Cursor 的时候你 ...
- android sharedpreferences 工具类,android sharedpreferences工具类
释放双眼,带上耳机,听听看~! 今天,简单讲讲如何写一个sharedpreferences的工具类. 很简单,把一些重复的操作封装在工具类里,其他地方调用就可以.在网上搜索了比较多的资料,找到一个比较 ...
- android动画送礼物,Android仿直播类app赠送礼物功能
直播界面 实现的是播放本地的视频文件: /** * 直播界面,用于对接直播功能 */ public class LiveFrag extends Fragment { private ImageVie ...
- android.hardware.Camera类及其标准接口介绍
android.hardware.Camera类及其标准接口介绍,API level 19 http://developer.android.com/reference/android/hardwar ...
最新文章
- JavaScript类的问题
- TaxonKit工具:获取物种NCBI数据库的Taxonomy ID
- TcpClient 有好多坑
- 科大星云诗社动态20210531
- mongoDB的常见命令
- cocos2d-x游戏实例(25)-简易动作游戏(3)
- dup和dup2的使用方法
- python中ln怎么表示_Python math库 ln(x)运算的实现及原理
- matplotlib text 文字处理
- 不能包含全角 正则_java常用正则表达式
- 太牛逼了!这个开源项目,可以把我从视频中移除!
- Flink 实战:如何解决生产环境中的技术难题?
- 企业办理CMMI认证是怎么收费的?
- 服务器系统网卡调节,使用智简魔方安装服务器系统时候如何在BIOS中开启网卡pxe...
- 用Qt图形视图框架开发拼图游戏
- Vue开源框架-vue-element-admin-任务列表项目
- 【ML/DL】python3学习《机器学习实战》书中的报错及解决办法
- SAP QM 检验点 (Inspection Point) 的使用
- 1.7-11:潜伏者
- 如何与人进行有效沟通
热门文章
- 开源:Swagger Butler 1.1.0发布,利用ZuulRoute信息简化配置内容
- Hadoop YARN:调度性能优化实践
- CVPR 2019轨迹预测竞赛冠军方法总结
- 圆形进度条以及百分率指示器 Scroller类的练习
- 知识图谱之语言计算与信息抽取
- 18-Gm-TransH:Group-Constrained Embedding of Multi-fold Relations in Knowledge Bases,嵌入,transH,n-ary
- 考研心路历程2021北京交通大学计算机学院软件工程
- Python Django项目部署 Linux 服务器
- python函数:基础函数调用整理
- web移动端开发经验总结