Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)
采取的方法是Fragment+FragmentTabHost组件来实现这种常见的app主页面的效果
首先给出main.xml文件
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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>
主代码:
publicclassMainActivity
{@ViewInject(android.R.id.tabhost)
privateFragmentTabHostmTabHost;
privateLayoutInflaterlayoutInflater;
privateintmImageViewArray[]={R.drawable.home_tab1,R.drawable.home_tab2,R.drawable.home_tab3,R.drawable.home_tab4};
privateStringmTextviewArray[]={"首页","圈子","资讯","个人中心"};
privateClassfragmentArray[]={Fragment1.class,Fragment2.class,Fragment3.class,Fragment4.class};
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
init();
}
@Override
protectedvoidinit(){
//list=newJSONArray();
layoutInflater=LayoutInflater.from(this);
initTabHost();//初始化底部菜单
}
/**
*初始化底部工具栏
*/
privatevoidinitTabHost(){
mTabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
intcount=fragmentArray.length;
for(inti=0;i<count;i++){
TabHost.TabSpectabSpec=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);
}
/**
*项的样式
*@paramindex第几个
*@return每一个Tab样式
*/
privateViewgetTabItemView(intindex){
Viewview=layoutInflater.inflate(R.layout.tab_home_item,null);
ImageViewimageView=(ImageView)view.findViewById(R.id.icon);
imageView.setImageResource(mImageViewArray[index]);
TextViewtextView=(TextView)view.findViewById(R.id.name);
textView.setText(mTextviewArray[index]);
returnview;
}
}
通过以上文章,希望能帮助到大家,谢谢大家对本站的支持!