码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-07 07:58:07      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

使用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;
    }

}

选中变色就不在啰嗦了 技术分享

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

标签:

原文地址:http://blog.csdn.net/chenxin_oo/article/details/51333368

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!