使用FragmentTabHost 完成一个简单的底部导航栏

使用FragmentTabhost+Fragment实现一个底部导航栏

主布局:

  //放置Fragment

  <FrameLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:id="@+id/fragment">

    </FrameLayout>

    <android.support.v4.app.FragmentTabHost

        android:id="@+id/tabhost"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content">

        <!--<FrameLayout-->

            <!--android:id="@+id/tabcontent"-->

            <!--android:layout_width="0dp"-->

            <!--android:layout_height="0dp"-->

            <!--android:layout_weight="0"-->

        <!--/>-->

    </android.support.v4.app.FragmentTabHost>

代码:

public class FragmentTabhost extends AppCompatActivity {

    private Class fragmentArray[] = {BlankFragment.class, BlankFragment1.class, BlankFragment2.class};

    private int mImageViewArray[] = {R.drawable.tag,R.drawable.thunder,R.drawable.umbrella};

    private String mTextviewArray[] = {"首页", "消息","更多"};

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fragment_tabhost);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        FragmentTabHost fg= (FragmentTabHost) findViewById(R.id.tabhost);

        fg.setup(this, getSupportFragmentManager(), R.id.fragment);

        int count = fragmentArray.length;

        //循环放置

        for(int i = 0; i < count; i++){

            TabHost.TabSpec tabSpec = fg.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));

            fg.addTab(tabSpec, fragmentArray[i], null);

        }

        //去掉了底部导航栏的间隔竖线

        fg.getTabWidget().setDividerDrawable(null);

    }

//给导航栏放置图片和标题

    private View getTabItemView(int index){

        View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_item_xml, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);

        imageView.setImageResource(mImageViewArray[index]);

        TextView textView = (TextView) view.findViewById(R.id.textview);

        textView.setText(mTextviewArray[index]);

        return view;

    }

}

选中变色就不在啰嗦了 

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本人初学Android,最近做了一个实现安卓简单音乐播放功能的播放器,收获不少,于是便记录下来自己的思路与知识总结...
    落日柳风阅读 19,514评论 2 41
  • Android使用底部导航 Android底部导航停留在屏幕底部,提供应用中顶级视图之间的导航。这是在具有向后兼容...
    ListenToCode阅读 2,641评论 1 15
  • 整体结构使用FragmentTabHost+Fragment搭建 如下图所示,整一个底部导航栏是一个Fragmen...
    kim_liu阅读 583评论 0 2
  • 小时候的学习有老师教,没曾想到长大后的婚姻生活也有老师教。当下感觉自己是幸福的,同时也是幸运的。 老师开过很多小灶...
    芳与华阅读 835评论 4 10
  • 初秋的校园另有一番美景。 在校园里 ,有几棵挺拔的雪松,雪松四季常青,使校园变得不那么的单凋。雪松的叶子...
    赵芷悦阅读 906评论 0 0

友情链接更多精彩内容