FragmentTabHost用法

摘要:
FragmentTabHost由Tabhost、TabWidget、切换内容容器FrameLayout层次关系组成----FragmentTabHost|-----TabWidget |-----FrameLayout布局实现Tabhost采用android.support.v4.app。FragmentTabHost Note-id:@android:id/tabhost实现tabWidget Note-id

FragmentTabHost用法第1张

FragmentTabHost组成

  1. Tabhost,TabWidget,切换的内容容器FrameLayout
  2. 层级关系

    ----FragmentTabHost
        |-----TabWidget
        |-----FrameLayout
    

布局实现

  1. 实现tabhost采用android.support.v4.app.FragmentTabHost

    注意 id:@android:id/tabhost

  2. 实现tabWidget

    注意 id:@android:id/tabs

  3. 实现FrameLayout

    注意

    1. id: @android:id/tabcontent

    2. 此容器已经被废除,但在布局中必须有

  4. 实现自定义的内容容器区域(FrameLayout)

    注意 :

    1. 整体需采用线性布局

    2. 将自定义展示的区域放到TabHost之上

    3. 自定义的内容需要给权重

代码实现

  1. 初始化TabHost

    调用setup(Context,FragmentManager,int);

    最后一个参数 指的是 Fragment的容器id 用来切换fragment的

  2. 新建TabSpec

    调用setIndicator(View view)//实现自定义的tab

  3. 添加TabSpec

    调用addTab(TabSpec,Class,Bundle)的方法添加TabSpec

    Class 指的是 tab对应的 Fragment

    Bundle 指的是 Fragment 初始化的参数

组合式控件的实现(下面的Tab一般都自定义)

  1. 新建布局
  2. 将布局和代码进行关联

    新建的View 必须继承 和 布局容器一样的容器类

    通过View.inflate(context,LayoutId, this)将View和xml进行绑定

  3. 功能进行封装

    根据当前View需要的功能进行封装

布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <FrameLayout
        android: 
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>
    <android.support.v4.app.FragmentTabHost
        android: 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TabWidget
            android: 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#FFF1F1F1" >
        </TabWidget>
        <FrameLayout
            android: 
            android:layout_width="0dp"
            android:layout_height="0dp" >
        </FrameLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

  代码:

public class HomeActivity extends BaseActivity implements OnTabChangeListener {
	private static final String TAB_CHAT = "chat";
	private static final String TAB_CONTACT = "contact";
	private static final String TAB_DISCOVER = "discover";
	private static final String TAB_ME = "me";
	private FragmentTabHost tabhost;
	private TabIndicatorView chatIndicator;
	private TabIndicatorView contactIndicator;
	private TabIndicatorView discoverIndicator;
	private TabIndicatorView meIndicator;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_home);
		// 1. 初始化TabHost
		tabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		tabhost.setup(this, getSupportFragmentManager(),
				R.id.activity_home_container);//上面的fargment
		// 2. 新建TabSpec
		TabSpec spec = tabhost.newTabSpec(TAB_CHAT);//需要自定义常量
		chatIndicator = new TabIndicatorView(this);
		chatIndicator.setTabTitle("消息");
		chatIndicator.setTabIcon(R.drawable.tab_icon_chat_normal,
				R.drawable.tab_icon_chat_focus);//选中和默认的图标
		spec.setIndicator(chatIndicator);
		// 3. 添加TabSpec
		tabhost.addTab(spec, ChatFra.class, null);//添加上面的fargment
		// 2. 新建TabSpec
		spec = tabhost.newTabSpec(TAB_CONTACT);
		contactIndicator = new TabIndicatorView(this);
		contactIndicator.setTabIcon(R.drawable.tab_icon_contact_normal,
				R.drawable.tab_icon_contact_focus);
		contactIndicator.setTabTitle("通讯录");
		contactIndicator.setTabUnreadCount(10);
		spec.setIndicator(contactIndicator);
		// 3. 添加TabSpec
		tabhost.addTab(spec, ContactFra.class, null);
		// 2. 新建TabSpec
		spec = tabhost.newTabSpec(TAB_DISCOVER);
		discoverIndicator = new TabIndicatorView(this);
		discoverIndicator.setTabIcon(R.drawable.tab_icon_discover_normal,
				R.drawable.tab_icon_discover_focus);
		discoverIndicator.setTabTitle("发现");
		discoverIndicator.setTabUnreadCount(10);
		spec.setIndicator(discoverIndicator);
		// 3. 添加TabSpec
		tabhost.addTab(spec, DiscoverFra.class, null);
		// 2. 新建TabSpec
		spec = tabhost.newTabSpec(TAB_ME);
		meIndicator = new TabIndicatorView(this);
		meIndicator.setTabIcon(R.drawable.tab_icon_me_normal,
				R.drawable.tab_icon_me_focus);
		meIndicator.setTabTitle("我");
		meIndicator.setTabUnreadCount(10);
		spec.setIndicator(meIndicator);
		// 3. 添加TabSpec
		tabhost.addTab(spec, MeFra.class, null);
		// 去掉分割线
		tabhost.getTabWidget().setDividerDrawable(android.R.color.white);
		// 初始化 tab选中
		tabhost.setCurrentTabByTag(TAB_CHAT);
		chatIndicator.setTabSelected(true);
		// 设置tab切换的监听
		tabhost.setOnTabChangedListener(this);
	}
	@Override
	public void onTabChanged(String tag) {
		chatIndicator.setTabSelected(false);
		contactIndicator.setTabSelected(false);
		discoverIndicator.setTabSelected(false);
		meIndicator.setTabSelected(false);
		if (TAB_CHAT.equals(tag)) {
			chatIndicator.setTabSelected(true);
		} else if (TAB_CONTACT.equals(tag)) {
			contactIndicator.setTabSelected(true);
		} else if (TAB_DISCOVER.equals(tag)) {
			discoverIndicator.setTabSelected(true);
		} else if (TAB_ME.equals(tag)) {
			meIndicator.setTabSelected(true);
		}
	}
}

FragmentTabHost用法第2张  FragmentTabHost用法第3张

下面的自定义,动态的去添加信息

public class TabIndicatorView extends RelativeLayout {
	private ImageView ivTabIcon;
	private TextView tvTabHint;
	private TextView tvTabUnRead;
	private int normalIconId;
	private int focusIconId;
	public TabIndicatorView(Context context) {
		this(context, null);//这个实现这个构造函数就可以了
	}
	public TabIndicatorView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// 将布局文件和 代码进行绑定
		View.inflate(context, R.layout.tab_indicator, this);
		ivTabIcon = (ImageView) findViewById(R.id.tab_indicator_icon);
		tvTabHint = (TextView) findViewById(R.id.tab_indicator_hint);
		tvTabUnRead = (TextView) findViewById(R.id.tab_indicator_unread);
		
		setTabUnreadCount(0);
	}
	// 设置tab的title
	public void setTabTitle(String title) {
		tvTabHint.setText(title);
	}
	public void setTabTitle(int titleId) {
		tvTabHint.setText(titleId);
	}
	// 初始化图标
	public void setTabIcon(int normalIconId, int focusIconId) {
		this.normalIconId = normalIconId;
		this.focusIconId = focusIconId;
		ivTabIcon.setImageResource(normalIconId);
	}
	// 设置未读数
	public void setTabUnreadCount(int unreadCount) {
		if (unreadCount <= 0) {
			tvTabUnRead.setVisibility(View.GONE);
		} else {
			if (unreadCount <= 99) {
				tvTabUnRead.setText(unreadCount + "");
			} else {
				tvTabUnRead.setText("99+");
			}
			tvTabUnRead.setVisibility(View.VISIBLE);
		}
	}
	// 设置选中
	public void setTabSelected(boolean selected) {
		if (selected) {
			ivTabIcon.setImageResource(focusIconId);
		} else {
			ivTabIcon.setImageResource(normalIconId);
		}
	}
}

  

免责声明:文章转载自《FragmentTabHost用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇API及接口清单Android平台Overlay机制下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

.NET跨平台机制一(mono for android配置教程)

    忙完了毕业设计,坐等毕业,终于有时间可以玩玩.NET的跨平台机制了,当然了.NET跨平台主要就是Linux,所以就蛮有心思的去配置了下mono for android的开发环境。       首先,准备工作要做足,运行时,虚拟机,模拟器都要先下载好了。 前期准备,先看看安卓模拟器的配置(已经会配置的略过..)配置教程网络上也很多,我这里就稍微盖过...

OpenGL实现多层绘制(Layered Rendering) [转]

http://blog.csdn.net/u010462297/article/details/50589991 引言 在某些情况下会需要用到多层绘制。FBO下有多个颜色挂接点(Color Attachment),可以用不同的挂接点挂接不同的纹理对象,实现绘制多张纹理(MRT),这在之前的文章里已经有所描述。但是有时候这种方法是不够好用的: - 当纹理非...

Xamarin 跨移动端开发系列(01) -- 搭建环境、编译、调试、部署、运行

   (本文是基于老版本的VS和Xamarin,而VS2017已经集成了Xamarin,所以,本文已经过时,最新的Xamarin开发介绍请参见 使用 Xamarin开发手机聊天程序 。)    如果是.NET开发人员,想学习手机应用开发(Android和iOS),Xamarin 无疑是最好的选择,编写一次,即可发布到Android和iOS平台,真是利器中的...

Android中ListView的总结(1)

这ListView真是麻烦,一个小小的FileBrowser废了将近2天。看来自己的功力还需要加强。 做FileBrowser中遇到几个ListView问题总结一下。 1、自定义样式   ListView其实和Asp.net里面的Repeater有点像,但是不同的是项的内容可以用一个layout文件来套用,这个比较有意思,毕竟刚开始研究android姑且叫...

android学习ScrollView的使用

ScrollView 的使用相对来讲比较简单,通过包含更多的布局文件,使得上下滑动可以浏览到更多内容。 关于ScrollView有几个点需要注意的地方 1,ScrollView的滚动方式 ScrollView有两种滚动方式,横向的和纵向的,一般横向的用的比较少。ScrollView控件默认就是纵向滚动的,如果需要横向滚动只需要更改标签 Horizontal...

Android杂谈关于Android的nodpi,xhdpi,hdpi,mdpi,ldpi

关于Android的nodpi,xhdpi,hdpi,mdpi,ldpi 首先是几个基本概念:1.屏幕尺寸Screen size即显示屏幕的实际大小,按照屏幕的对角线进行测量。为简单起见,Android把所有的屏幕大小分为四种尺寸:小,普通,大,超大(分别对应:small, normal, large, and extra large).应用程序可以为这四...