fragmentTabhost+Fragment底部菜单实现方法
2016-01-10 19:55
513 查看
最近在研究底部菜单的实现方法,之前用的是viewpager+fragment和radiobutton+fragment,但是代码非常多,谷歌在最新的api上用了fragmentTabhost实现起来比较简单,好了废话不多说,先看运行效果。
1.今天的知识点:fragmentTabhost的用法和selector的用法
下面直接上代码:
activity_main.xml
tab_indicator.xml
1.今天的知识点:fragmentTabhost的用法和selector的用法
下面直接上代码:
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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/maincontent" 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"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"> </FrameLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>
tab_indicator.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" android:paddingBottom="3dp" android:paddingTop="3dp"> <ImageView android:id="@+id/image_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name"/> <TextView android:id="@+id/text_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dp"/> </LinearLayout>
MainActivity.java
packagebailead.com.sports;
importandroid.os.Bundle;
importandroid.support.v4.app.FragmentActivity;
importandroid.support.v4.app.FragmentTabHost;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.TabHost;
importandroid.widget.TextView;
importjava.util.ArrayList;
importjava.util.List;
importbailead.com.sports.entity.Tab;
importbailead.com.sports.fragment.HomeFragment;
importbailead.com.sports.fragment.IntroFragment;
importbailead.com.sports.fragment.SettingFragment;
/**
*CreatedbyAdministratoron2016/1/9.
*/
publicclassMainActivityextendsFragmentActivity{
privateFragmentTabHostmTabHost;
privateLayoutInflatermInflater;
privateList<Tab>list=newArrayList<>();
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTab();
}
privatevoidinitTab(){
Tabhome=newTab(R.string.string_tab_home,R.drawable.selcet_image_home,HomeFragment.class);
Tabintro=newTab(R.string.string_tab_intro,R.drawable.selcet_image_intro,IntroFragment.class);
Tabsetting=newTab(R.string.string_tab_setting,R.drawable.selcet_image_setting,SettingFragment.class);
list.add(home);
list.add(intro);
list.add(setting);
mInflater=LayoutInflater.from(MainActivity.this);
mTabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(MainActivity.this,getSupportFragmentManager(),R.id.maincontent);
for(Tabtab:list){
TabHost.TabSpectabSpec=mTabHost.newTabSpec(String.valueOf(tab.getTitle()));
tabSpec.setIndicator(buildIndicator(tab));
mTabHost.addTab(tabSpec,tab.getFragment(),null);
}
//去掉分割线
mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
mTabHost.setCurrentTab(0);
}
publicViewbuildIndicator(Tabtab){
Viewview=mInflater.inflate(R.layout.tab_indicator,null);
ImageViewimageView=(ImageView)view.findViewById(R.id.image_tab);
TextViewtextView=(TextView)view.findViewById(R.id.text_tab);
imageView.setBackgroundResource(tab.getImage());
textView.setText(tab.getTitle());
returnview;
}
}
相关文章推荐
- 数据库--保证数据完整型
- numpy Windows 64位
- 前端call 和apply(javascript学习笔记)
- java中substring函数
- 百度地图 绘制路线图有多个方案时 实现点击切换路线
- heroku run rake db:migrate: No such file or directory
- AndroidStudio常用设置
- org.hibernate.MappingException: No Dialect mapping for JDBC type: -1
- 第7课在自己搭建的spark集群下运行程序实例
- 软件工程M1/M2总结
- SQL Server 全局变量
- Xamarin nuget package update 错误
- 数据库--管理数据库和表
- 多服务器之间session共享
- Linux(Ubuntu)下如何安装JDK
- bootstrap表单
- 不会改变操作对象内容的STL算法(元素查找,序列查找,统计元素个数,比较区间,找最值)
- 实践干货!猿题库 iOS 客户端架构设计
- 硬盘的读写原理
- vim中的复制粘贴问题