您的位置:首页 > 其它

FragmentTabHost+Fragment实现底部菜单的切换

2016-07-08 10:16 309 查看
添加依赖

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息