Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)
更新时间:2016年09月09日 09:57:55 投稿:lqh
本文主要介绍Fragment+FragmentTabHost组件实现常见主页面,这里整理了详细资料及简单示例代码,有兴趣的小伙伴可以参考下
采取的方法是Fragment+FragmentTabHost组件来实现这种常见的app主页面的效果
首先给出main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/color_home_tab_line" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/et_divider_disable">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout> </LinearLayout>
主代码:
public class MainActivity
{ @ViewInject(android.R.id.tabhost)
private FragmentTabHost mTabHost;
private LayoutInflater layoutInflater;
private int mImageViewArray[] = {R.drawable.home_tab1, R.drawable.home_tab2, R.drawable.home_tab3, R.drawable.home_tab4};
private String mTextviewArray[] = {"首页", "圈子", "资讯","个人中心"};
private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class,Fragment4.class};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
@Override
protected void init() {
// list=new JSONArray();
layoutInflater=LayoutInflater.from(this);
initTabHost();//初始化底部菜单
}
/**
* 初始化底部工具栏
*/
private void initTabHost() {
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
int count = fragmentArray.length;
for (int i = 0; i < count; i++) {
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
.setIndicator(getTabItemView(i));
mTabHost.addTab(tabSpec, fragmentArray[i], null);
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.color.white);
}
mTabHost.setCurrentTabByTag(mTextviewArray[0]);
mTabHost.getTabWidget().setDividerDrawable(null);
}
/**
* 项的样式
* @param index 第几个
* @return 每一个Tab样式
*/
private View getTabItemView(int index) {
View view = layoutInflater.inflate(R.layout.tab_home_item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
imageView.setImageResource(mImageViewArray[index]);
TextView textView = (TextView) view.findViewById(R.id.name);
textView.setText(mTextviewArray[index]);
return view;
}
}
通过以上文章,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
Android数据流之Channel和Flow实现原理和技巧详解
在 Android 应用程序的开发中,处理异步数据流是一个常见的需求,为了更好地应对这些需求,Kotlin 协程引入了 Channel 和 Flow,它们提供了强大的工具来处理数据流,本文将深入探讨 Channel 和 Flow 的内部实现原理、高级使用技巧以及如何在 Android 开发中充分利用它们2023-11-11
详解如何使用Android Studio 进行NDK开发和调试
本篇文章主要介绍了详解如何使用Android Studio 进行NDK开发和调试,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12
Android开发之获取LayoutInflater对象的方法总结
这篇文章主要介绍了Android开发之获取LayoutInflater对象的方法,结合实例形式总结分析了Android获取LayoutInflater对象的常用技巧,需要的朋友可以参考下2016-02-02
详解android特性之CoordinatorLayout用法探析实例
本篇文章主要介绍了android特性之CoordinatorLayout用法探析实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-02-02


最新评论