FragmentTabHost使用
来源:互联网 发布:刷机后恢复数据 编辑:程序博客网 时间:2024/05/12 18:38
1.布局实现上面是fragment下面是button的(默认切换在上面)FragmentTabHost包裹的FrameLayout也要写,不让它显示即可,FragmentTabHost内的所有id也要用系统的id
<?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" > <FrameLayout android:id="@+id/<span style="font-family: Arial, Helvetica, sans-serif;">activity_home_container</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span> android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#FFF1F1F1" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost></LinearLayout>
2.代码调用,需要继承FragmentActivity
// 1. 初始化TabHosttabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);tabhost.setup(this, getSupportFragmentManager(),R.id.activity_home_container);// 2. 新建TabSpecTabSpec 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. 添加TabSpectabhost.addTab(spec, ChatFra.class, null);// 2. 新建TabSpecspec = 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. 添加TabSpectabhost.addTab(spec, ContactFra.class, null);// 2. 新建TabSpecspec = 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. 添加TabSpectabhost.addTab(spec, DiscoverFra.class, null);// 2. 新建TabSpecspec = 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. 添加TabSpectabhost.addTab(spec, MeFra.class, null);// 去掉分割线tabhost.getTabWidget().setDividerDrawable(android.R.color.white);// 初始化 tab选中tabhost.setCurrentTabByTag(TAB_CHAT);chatIndicator.setTabSelected(true);// 设置tab切换的监听tabhost.setOnTabChangedListener(this);}@Overridepublic 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);}}3.自定义的TabIndicator
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的titlepublic 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);}}}
1 1
- 【FragmentTabHost】FragmentTabHost的使用
- FragmentTabHost 使用;
- FragmentTabHost使用
- FragmentTabHost使用
- FragmentTabHost的使用
- FragmentTabHost的使用
- fragmentTabHost的使用
- 使用FragmentTabhost代替Tabhost
- Android FragmentTabHost使用
- FragmentTabHost的使用
- android FragmentTabHost的使用
- FragmentTabHost使用总结
- FragmentTabHost的使用
- FragmentTabHost的简单使用
- FragmentTabhost的使用
- FragmentTabHost 菜单使用
- FragmentTabHost的使用
- FragmentTabHost的使用
- 深复制体验
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
- JDK TimerTask任务使用
- canvas变换
- 如果创业成功都比不上躺着炒房,年轻人还有什么希望?
- FragmentTabHost使用
- object-c中类的继承
- JavaScript代理模式之图片预加载
- Spring MVC 和 struts MVC的区别。
- 集群批量安装jdk
- JAVA中用二分法实现数组中的数据调换
- pl/sql中TYPE与ROWTYPE区别
- LPC1788 引脚分析
- 258AddDigits