Android FragmentTabHost + Fragm
2017-11-07 本文已影响0人
AKyS佐毅
实现效果

FragmentTabHost介绍
- FragmentTabHost代表整个底部导航栏,它包含的每一个Item,称之为TabSpec,点击会切换到每一个分页。
- TabSpec里面需要有指示器Indicator,用来设置样式。Inditcator 一般都会包裹一个View.包含图片和文字信息。
使用步骤如下:
- 1、在mainActivity中使用如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--实际装Fragment的容器Fragment-->
<FrameLayout
android:id="@+id/real_tabhost"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<!--定义FragmentTabHost控件-->
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" ><!--表面装Fragment的容器-->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" /><!--装Tab的内容-->
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
- 2、为了方便添加底部图片文字。定义Tabitem类

- 3、获取FragmentTabHost实例

-4、填充内容


- 5、item的点击事件

- 6、点击item图标转换
- 当点击图标的时候,图标和文字都还不会变成红色。首先设置图标。
- 在Drawable文件下编写xml文件,都是设置成当被点的时候改变图标,使用准备好的红色的图标
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/home_icon_25" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/home_icon_selected_25" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@mipmap/home_icon_selected_25" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@mipmap/home_icon_selected_25" />
<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@mipmap/home_icon_selected_25" />
<item android:state_pressed="true" android:drawable="@mipmap/home_icon_selected_25" />
</selector>
效果图如下:
