欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)

程序员文章站 2024-03-06 19:10:56
采取的方法是fragment+fragmenttabhost组件来实现这种常见的app主页面的效果 首先给出main.xml文件

采取的方法是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;
   }
 
 
 
 
 }

通过以上文章,希望能帮助到大家,谢谢大家对本站的支持!