FragmentTabHost 实现底部android底部菜单的切换

效果图:


Android FragmentTabHost的简单使用
布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <FrameLayout        android:id="@+id/realtabcontent"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" />    <android.support.v4.app.FragmentTabHost        android:id="@android:id/tabhost"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/maintab_toolbar_bg" >        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp" />    </android.support.v4.app.FragmentTabHost></LinearLayout>

MainActivity:

public class MainActivity extends FragmentActivity {private FragmentTabHost mFragmentTabHost;private LayoutInflater mInflater;private String[] tabs = { "推荐", "仓库", "搜索", "我的" };private int[] drawables = { R.drawable.tab_home_btn,R.drawable.tab_more_btn, R.drawable.tab_square_btn,R.drawable.tab_selfinfo_btn };private Class<?>[] fragments = { HomeFragment.class, ClassisFragment.class,SearchFragment.class, SelfishFragment.class };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mContext=this;mInflater=getLayoutInflater();mFragmentTabHost=(FragmentTabHost) findViewById(android.R.id.tabhost);mFragmentTabHost.setup(this, getSupportFragmentManager(),R.id.realtabcontent);for (int i = 0; i < fragments.length; i++) {mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec(tabs[i]).setIndicator(getTabItemView(i)), fragments[i], null);mFragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);}}/* * 为每个TabItem添加显示信息 */private View getTabItemView(int i) {View view = mInflater.inflate(R.layout.tab_item_view, null);ImageView image = (ImageView) view.findViewById(R.id.tab_item_image);image.setImageResource(drawables[i]);TextView text = (TextView) view.findViewById(R.id.tab_item_text);text.setText(tabs[i]);return view;}}



更多相关文章

  1. android 使用私有maven仓库发布、打包、引用
  2. [android]初始化代码仓库时出现“OSError: [Errno 2] No such fi
  3. android仓库 用到的网址。
  4. Android之系统自带的文字外观设置及实际显示效果图 android:text
  5. Android中gravity的各种效果图
  6. Android中使用Gallery_tabhost来实现标签效果图源码
  7. Android源码仓库和Repo工具使用
  8. android 使用xml selector设置按钮点击效果图片
  9. Android实时监控项目第一篇:项目分析及AVD模拟效果图

随机推荐

  1. Android(安卓)6.0棉花糖新特性
  2. Android撬动IT市场的新支点
  3. Android应用程序窗口(Activity)的运行上下
  4. Android缺乏整体控制或成发展障碍
  5. Android中View的getX,getY...
  6. android常用UI控件的使用例子
  7. Android能用Linux打败Linux手机吗?
  8. Android撬动IT市场的新支点
  9. Android支付之支付宝封装类
  10. Android中实现滑动的七种方式总结