Android 底部TabActivity(1)——FragmentActivity
先看看效果图:
第一篇Tab系列的文章首先实现这样的风格的底部Tab:背景条颜色不变,我们是用了深灰的颜色,图标会发生对应的变化。当选中某个标签后该标签的背板会由正常的颜色变为不正常,哈哈,是变为加深的灰色,更加凸显当前页的效果,所以我比較这样的类型。在这里文字的变化我没处理,假设变色使用个selector就攻克了。这里不再赘述。
再看一下整个Project的结构。例如以下
以下逐一介绍一下实现过程,详细实现还是看凝视吧,代码也不是非常多,就不啰嗦了。
step1:首先是主界面MainTabActivity.java
package sun.geoffery.fragmenttabhost; 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.TabHost.TabSpec;
import android.widget.TextView; /**
* All rights Reserved, Designed By GeofferySun
* @Title: MainTabActivity.java
* @Package sun.geoffery.fragmenttabhost
* @Description:自己定义TabHost
* @author: GeofferySun
* @date: 2014-9-28 下午11:33:15
* @version V1.0
*/
public class MainTabActivity extends FragmentActivity {
// 定义FragmentTabHost对象
private FragmentTabHost mTabHost; // 定义一个布局
private LayoutInflater mInflater; // 定义数组来存放Fragment界面
private Class mFragmentAry[] = { FragmentPage0.class, FragmentPage1.class,
FragmentPage2.class, FragmentPage3.class, FragmentPage4.class }; // 定义数组来存放button图片
private int mImgAry[] = { R.drawable.sl_rbtn_home,
R.drawable.sl_rbtn_atme,
R.drawable.sl_rbtn_msg,
R.drawable.sl_rbtn_square,
R.drawable.sl_rbtn_data }; // Tab选项卡的文字
private String mTxtAry[] = { "首页", "@我", "消息", "广场", "资料" }; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_layout); initView();
} /**
* 初始化组件
*/
private void initView() {
// 实例化布局对象
mInflater = LayoutInflater.from(this); // 实例化TabHost对象。得到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); // 得到fragment的个数
int count = mFragmentAry.length; for (int i = 0; i < count; i++) {
// 为每个Tabbutton设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(mTxtAry[i]).setIndicator(getTabItemView(i));
// 将Tabbutton加入进Tab选项卡中
mTabHost.addTab(tabSpec, mFragmentAry[i], null);
// 设置Tabbutton的背景
mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);
}
} /**
* 给Tabbutton设置图标和文字
* @param index
* @return
*/
private View getTabItemView(int index) {
View view = mInflater.inflate(R.layout.tab_item_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImgAry[index]); TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTxtAry[index]); return view;
}
}
step2:每个标签页相应的页面FragmentPage0.java
package sun.geoffery.fragmenttabhost; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FragmentPage0 extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_0, null);
}
}
step3:单选标签的背板文件selector_tab_background.xml
<?xml version="1.0" encoding="utf-8"? >
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/bg_bottombar_active" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_bottombar_active" android:state_selected="true"/> </selector>
step4:标签的图标sl_rbtn_atme.xml。有点击效果就要这么搞
<?xml version="1.0" encoding="utf-8"? ><!-- tab栏button -->
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/sl_rbtn_atme_on" android:state_selected="true" />
<item android:drawable="@drawable/sl_rbtn_atme_off" /> </selector>
step5:主界面布局main_tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/bg_bottombar" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost> </LinearLayout>
step6:每一个标签的布局tab_item_view.xml,上边一个图标,下边一个文字
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:focusable="false"
android:padding="3dp"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="7dp"
android:paddingTop="3dp"
android:text="@string/app_name"
android:textColor="#ffffff"
android:textSize="12sp" /> </LinearLayout>
step7:每一个标签相应页面的布局fragment_0.xml
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="首页"
android:gravity="center"
android:textSize="20sp"
android:textColor="#403901"/> </LinearLayout>
OK,到此为止,就完事儿了,以上步骤没有依照开发顺序来,但还是应该能看懂的吧,哈哈,看不懂请留言咪我。
Android 底部TabActivity(1)——FragmentActivity的更多相关文章
- Android 底部TabActivity(0)——开篇(界面分析|系列文章文件夹)
当下主流的软件没有一个统一明白的风格,App框架什么样的都有,但个人钟情于页面底部Tab分签架构,移动设备的屏幕尽管越来越大,可是显示的内容还是有限,为了能展示很多其它的内容,方便简洁的操作习惯中Ta ...
- Android底部TabHost API
今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...
- VS 2015 开发Android底部导航条----[实例代码,多图]
1.废话背景介绍 在Build 2016开发者大会上,微软宣布,Xamarin将被整合进所有版本的Visual Studio之中. 这也就是说,Xamarin将免费提供给所有购买了Visual ...
- Android底部导航栏——FrameLayout + RadioGroup
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6285881.html Android底部导航栏有多种实现方式,本文详细介绍FrameLayout ...
- Android底部导航栏创建——ViewPager + RadioGroup
原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...
- Android底部导航栏
Android底部导航栏 今天简单写了一个底部导航栏,封装了一个库,用法比较简单 效果图 Github地址:https://github.com/kongqw/KqwBottomNavigation ...
- BottomBar之Android底部菜单
BottomBar之Android底部菜单 前言:开源项目BottomBar,实现Android底部菜单(常用菜单,BottomBar实现动画(上下式)+消息菜单,BottomBar+ViewPage ...
- android 底部菜单栏实现(转)
1.Android学习之BottomNavigationBar实现Android特色底部导航栏 2.Android底部导航栏的四种实现 3.Android BottomNavigationBar底部导 ...
- Android底部菜单的实现
前言:以前制作菜单使用TabHost,但是android 3.0以上就被废弃了,google已经不建议使这个类了.ActionBar也是菜单,不过在头部,算是导航了 ===本文就介绍怎么制作底部菜单= ...
随机推荐
- Linux命令之chgrp
chgrp [选项] … GROUP FILE … chgrp [选项] … --reference=RFILE FILE … chgrp命令是用来改变文件的组所有权.将改变每一个FILE的所属组为G ...
- 35、Flask实战第35天:权限设计
二进制及其相关运算 认识二进制 0,1,2,3,4,5,6,7,8,9,10:逢10进1 0,1:逢2进1 二进制转十进制 十进制 二进制 0 0 1 1 2 10 3 11 4 100 255 11 ...
- 内存分哪些区 C++,ios,java
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)—由编译器自动分配释放,存 ...
- [BZOJ2458][BeiJing2011]最小三角形(分治)
求平面上n个点组成的周长最小的三角形. 回忆平面最近点对的做法,找到横坐标的中点mid分治到两边,合并时考虑离mid横坐标不超过当前最小值d的所有点,按y排序后暴力更新答案. 这个题也一样,先分治到两 ...
- 【线段树】Gym - 101201J - Shopping
题意:n个数,m次询问,每次给你一个询问v,l,r,问你v%a[l]%a[l+1]%...%a[r]是多少. a%b,结果要么不变,要么至少缩小到a的一半,于是用线段树,每次询问当前区间最靠左侧的小于 ...
- 【扩展欧几里得】Codeforces Round #406 (Div. 2) A. The Monster
扩欧,a+bx=c+dx,输出x>=0且y>=0,且a+bx最小的解. 要注意不能只保证x非负,还得看看能否保证y也非负. #include<cstdio> #include& ...
- 【线段树/区间开平方】BZOJ3211-花神游历各国
[题目大意] 给出一些数,有两种操作.(1)将区间内每一个数开方(2)查询每一段区间的和 [思路] 普通的线段树保留修改+开方优化.可以知道当一个数为0或1时,无论开方几次,答案仍然相同.所以设置fl ...
- [NOIp2016提高组]组合数问题
题目大意: 给定n,m和k,对于所有的0<=i<=n,0<=j<=min(i,m)有多少对(i,j)满足C(j,i)是k的倍数. 思路: 先预处理出组合数,再预处理一下能整除个 ...
- [BZOJ1004](HNOI 2008) Cards
Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目 前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun很快就给出了答案.进一步,小春要求染出Sr张 ...
- MYSQL复习笔记4-基本SQL语句
Date: 20140115Auth: Jin参考:http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#select一:数据库操作1:显示数据 ...