android学习——底部标签栏,导航栏

本文详细介绍了在Android开发中实现底部标签栏的四种方法:TabActivity、ActivityGroup、FragmentActivity以及使用Toolbar+ViewPager。分别阐述了每种方法的布局文件和Java代码实现,总结中指出FragmentActivity是官方推荐的更灵活的选择。
摘要由CSDN通过智能技术生成


前言

这里介绍三种实现底部标签栏的方法——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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不被定义的号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值