FragmentTabHost+Fragment实现底部菜单的切换
2016-07-08 10:16
363 查看
添加依赖
compile 'com.android.support:support-v4:24.0.0' compile 'com.android.support:design:24.0.0'
先看看运行结果
搭建主布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.cewidgets.MainActivity"> <FrameLayout android:id="@+id/showTabFragmentContents"--》存放底部菜单切换时,触发的相应的Fragment android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> <android.support.v4.app.FragmentTabHost android:id="@+id/tabHost" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/black"> <FrameLayout android:layout_width="0dp"--》不设置为零的话底部菜单栏不会显示 android:layout_height="0dp" android:layout_weight="0"></FrameLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>
搭建子布局 tabhost_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" --》全部充满屏幕 android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:padding="5dp"> <ImageView android:id="@+id/tabHostItem_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_home_green_400_24dp" /> --》图片选择器 <TextView android:id="@+id/tabHostItem_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/tab_iv_color_selector" -->字体颜色选择器 android:layout_marginTop="1dp" android:text="首页" /> </LinearLayout>
在Activity中 MainActivity.java
public class MainActivity extends AppCompatActivity { private FragmentTabHost tabHost; private int[] tabHostItem_img = {R.drawable.tab_shouye_iv_seletor, R.drawable.tab_fenlei_iv_seletor, R.drawable.tab_mes_iv_seletor, R.drawable.tab_mine_iv_seletor}; private String[] tabHostItem_tv = {"首页", "分类", "消息", "我的"}; private Class[] tabHostFragments = {ShouYeFragment.class, FenLeiFragment.class, MsgFragment.class, MineFragment.class}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFragmentTabHost(); } private void initFragmentTabHost() { tabHost= (FragmentTabHost) findViewById(R.id.tabHost); tabHost.setup(this,getSupportFragmentManager(),R.id.showTabFragmentContents); for (int i=0;i<tabHostItem_img.length;i++){ TabHost.TabSpec tabSpec=tabHost.newTabSpec(tabHostItem_tv[i]).setIndicator(getTabHostView(i)); tabHost.addTab(tabSpec,tabHostFragments[i],null); } } private View getTabHostView(int i) { View view= LayoutInflater.from(this).inflate(R.layout.tabhost_item,null); ImageView iv= (ImageView) view.findViewById(R.id.tabHostItem_iv); iv.setImageResource(tabHostItem_img[i]); TextView tv= (TextView) view.findViewById(R.id.tabHostItem_tv); tv.setText(tabHostItem_tv[i]); return view; } }
相关文章推荐
- 详解Android TabHost的多种实现方法 附源码下载
- android 选项卡(TabHost)如何放置在屏幕的底部
- Android Fragment 基本了解(图文介绍)
- Android入门之TabHost与TabWidget实例解析
- Android程序开发之Fragment实现底部导航栏实例代码
- Android应用开发中Fragment与Activity间通信示例讲解
- Android应用中使用TabHost组件继承TabActivity的布局方法
- Android的Fragment的生命周期各状态和回调函数使用
- 浅谈Android App开发中Fragment的创建与生命周期
- 实例探究Android开发中Fragment状态的保存与恢复方法
- Android Fragment的使用方法(翻译)
- android TabHost(选项卡)的使用方法
- 实例探究Android应用编写时Fragment的生命周期问题
- Android App开发中创建Fragment组件的教程
- Android中的Fragment类使用进阶
- 使用Fragment来处理Andoird app的UI布局的实例分享
- Android App中使用ListFragment的实例教程
- Android Tabhost使用方法详解
- Android中Fragment子类及其PreferenceFragment的创建过程演示
- 自定义RadioButton和ViewPager实现TabHost带滑动的页卡效果