前言
这里介绍三种实现底部标签栏的方法——tabactivity,activitygroup,fragmentactivity/font>
一、标签按钮
- 标签按钮是一种可以通过点击切换不同的状态的自定义按钮,主要有两部分组成——selector,layout
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" />
<item android:drawable="@drawable/tab_bg_normal" />
</selector>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<CheckBox
android:id="@+id/ck_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:checked="false"
android:text="选定标签按钮"
android:textColor="@color/black"
android:textSize="17sp" />
<View
android:layout_width="match_parent"
android:layout_height="10dp" />
<!-- 注意这个文本视图的背景、文字颜色和顶部图标都采用了状态图形,使其看起来像个崭新的标签控件 -->
<TextView
android:id="@+id/tv_tab_button"
android:layout_width="100dp"
android:layout_height="60dp"
android:padding="5dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/tab_bg_selector"
android:text="点我"
android:textSize="12sp"
android:textColor="@drawable/tab_text_selector"
android:drawableTop="@drawable/tab_first_selector" />
</LinearLayout>
二、TabActivity
- 布局文件
<!-- 该方式的底部标签栏,根布局必须是TabHost,且id必须为@android:id/tabhost -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 内容页面都挂在这个框架布局下面 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/tabbar_height" />
<!-- 这是例行公事的选项部件,实际隐藏掉了 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- 下面是事实上的底部标签栏,采取水平线性布局展示 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/tabbar_height"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:orientation="horizontal">
<!-- 第一个标签控件 -->
<LinearLayout
android:id="@+id/ll_first"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="@style/TabButton"
android:drawableTop="@drawable/tab_first_selector"
android:text="@string/menu_first" />
</LinearLayout>
<!-- 第二个标签控件 -->
<LinearLayout
android:id="@+id/ll_second"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="@style/TabButton"
android:drawableTop="@drawable/tab_second_selector"
android:text="@string/menu_second" />
</LinearLayout>
<!-- 第三个标签控件 -->
<LinearLayout
android:id="@+id/ll_third"
android:layout_width