FragmentTabHost、Fragment实现底部点击切换Tab页面

Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换 一、问题描述   在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:http://blog.csdn.net/jerehedu/article/details/46911955 ),接下来我们使用FragmentTabHost+Fragment实现底部tab切换,效果如图所示 二、案例主要组件 阅读详情

1.MainActivity代码

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {

    private FragmentTabHost fragmentTabHost;
    private String texts[] = { "首页", "消息", "好友", "广场" };
    private int imageButton[] = { R.drawable.tab_home_selector,
            R.drawable.tab_gov_selector, R.drawable.tab_smart_selector,
            R.drawable.tab_setting_selector };

    private Class fragmentArray[] = { FragmentPage1.class, FragmentPage2.class,
            FragmentPage3.class, FragmentPage4.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.maincontent);

        for(int i=0;i<texts.length;i++){

             TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
             fragmentTabHost.addTab(spec, fragmentArray[i], null);
             fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bt_selector);
        }

    }

    private View getView(int i){
        View view = View.inflate(MainActivity.this,R.layout.tabcontent, null);


         ImageView imageView=(ImageView) view.findViewById(R.id.image);
         TextView textView=(TextView) view.findViewById(R.id.text);
         imageView.setImageResource(imageButton[i]);
         textView.setText(texts[i]);
        return view;
    }
}

2.①②③④Frgment就调用文字不一样的布局

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentPage1 extends Fragment {
    private TextView tv_title;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = View.inflate(getActivity(), R.layout.fragment, null);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("小玮");
//      return inflater.inflate(R.layout.fragment, null);
        return view;
    }
}

3.不同按钮点击状态标记布局就对应的图片不一样

这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/govaffairs_press" android:state_checked="true"/>
    <item android:drawable="@drawable/govaffairs"/>

</selector>

4.layout_main布局

<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" >
    <!-- 存放主要页面内容 -->
    <FrameLayout
        android:id="@+id/maincontent"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>
    <!-- 底层菜单 -->
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_tab_bg" >
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" >
        </FrameLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

5.底部菜单 按钮布局 图片+文字

这里写图片描述

<?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:orientation="vertical" 
    android:gravity="center_horizontal">
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:textColor="@android:color/white" />
</LinearLayout>

6.①②③④fragment布局 就文字不一样

<?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:orientation="vertical">
    <include layout="@layout/title_bar" /><!--actionBar-->
    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
        <TextView
            android:id="@+id/text"
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="我是第一个Fragment"
            android:textSize="20dp" />
    </FrameLayout>
</LinearLayout>
<include layout="@layout/title_bar" /><!--actionBar-->

控件标题

**title_bar.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_red_bg" >
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#fff"
        android:textSize="22sp" />
</RelativeLayout>

7.Demo下载

http://download.csdn.net/detail/playsit/9603278

FragmentTabHost+Fragment实现底部导航栏,仿淘宝 /** * 给Tab按钮设置图标和文字 */ private View getTabItemView(int index){ View view = layoutInflater.inflate(R.layout.tab_item_view, null); ImageView imageView = (Ima 阅读详情

相关推荐

Android 使用 TabLayout 切换 Fragment

Log.e(TAG, "onTabSelected: 暴击 " + tab.getText() + " 位置: " + index);Log.e(TAG, "Tab 释放,并且切换最后一次点击位置: " + index);android:theme="@style/ThemeOverlay.犀慕.FullscreenContainer"Log.d(TAG, "Tab 切换出错" + e.getMessage());android:text="通知" />android:text="审核" />

1378

android fragment 实现tab切换,开源中国Android客户端-使用Tab切换Fragment

一、简述在开源中国客户端中,使用了大量的列表去加载新闻、咨询、博客等等内容,而根据合理的分类,又可以使得这些列表显示的样式保持一致。说明:1)、可以左右滑动切换,更方便。2)、左右切换的时候,不去刷新,采用的是每个页面单独下拉刷新。例如,我的收藏夹: 在我的收藏夹,总共有5个Tab,也可以说需要切换5个视图,这些视图完全一样,只是数据大小不一。1、第一种处理办法:使用5个不同的Fragment添加...

weixin_42234168的博客 1078

Android Tab切换之Fragment实现方法

在Android开发中,Tab切换是一种常见的界面交互方式,可以让用户方便地在不同的功能页面之间进行切换。而Fragment是Android中用来构建灵活、可重用的界面组件的一种机制。本文将介绍如何使用Fragment实现Android Tab切换功能。以上就是使用Fragment实现Android Tab切换的方法。通过创建多个Fragment和PagerAdapter,配合ViewPager和TabLayout,可以轻松实现Tab切换的效果。Android Tab切换之Fragment实现方法。

HackDashX的博客 764

Android Tab切换之Fragment方法

纵观现在大部分Android客户端界面框架基本都是TitleBar+内容区域+TabBar的模式。其中TabBar用来切换对应的内容区域。 今天我们用Fragment的方式实现Tab的切换 不多说~上图: 点击底部的Tab,中间的Fragment内容区域就会相应的切换。 这里的Fragment只是简单的显示一个TextView。 Title的布局title_ui.xml:

my9074的专栏 4807

Android Tab切换:ViewPager与Fragment技术实现及优化

本文还有配套的精品资源,点击获取 简介:在Android开发中,实现Tab切换与页面滑动是UI设计和用户体验的重要组成部分。本文深入探讨了几种主流的实现策略,包括ViewPager与PagerAdapter的组合、ViewPager与Fragment及FragmentPagerAdapter的结合,以及传统的TabHost与TabWidget。详细介绍了ViewPager的...

weixin_29903713的博客 1644

基于FragmentTabHost+Fragment仿淘宝底部导航栏实战

在 Android 应用开发中,UI 的可维护性、扩展性和性能表现往往始于良好的 XML 布局设计。对于使用实现底部导航的场景,合理的布局结构不仅决定了视觉呈现效果,更直接影响到后续 Java/Kotlin 代码对控件的绑定与交互逻辑的稳定性。本章将深入剖析如何通过标准且高效的 XML 结构来定义一个完整可用的导航界面,涵盖从根容器选择、控件声明规范、Tab 项复用机制到内容区域规划等关键环节。为了获得完全控制权,应使用View作为指示器内容。首先需创建一个独立的布局文件。

weixin_29301059的博客 753

android viewpager 底部tabhost,FragmentTabHost+ViewPager实现底部导航栏

首先使用FragmentTabHost实现底部菜单栏。content_act_main.xmlxmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height...

weixin_30162121的博客 273

安卓FragmentTab选项卡相关-Fragment实现tab切换不坑自用.zip

Fragment 实现tab切换(不坑,自用).zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

谈谈Fragment的用法之Fragment实现Tab切换中的那些事

首先底部的我们使用FragmentTabHost即可,这里我们对系统的这个控件做了简单的修改。系统的这个控件在切换tab的时候是会detach 当前的Fragment, 也就是销毁当前Fragment的视图。这样就会导致每次切换tab的时候都会重新走onCreateView,重新创建Fragment view。这样我们之前的状态就会丢失,这当然不是我们所想要的。

wjr1949的博客 2211

Android的Fragment实现Tab切换

之前做项目零基础开始了Android开发,期间用到了Fragment的Tab切换功能,但网上好多帖子甚至代码都有点错误,于是我自己写了一个,希望能帮助到大家

fragment+radioButton实现底部菜单切换fragment的效果

利用fragmentTransaction的show和hide实现fragment+radioButton 切换的简单示例

FragmentTabHost+Fragment实现底部导航栏功能

一、效果图 二、FragmentTabHost+Fragment实现 1.activity布局文件 ​ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http...

XC_main的专栏 402

安卓实现首页tab切换(两种方式)

实现了点击tab切换内容的功能,代码简洁明了,适合初学者。其中一个利用radioGroup实现,另外一个通过普通的button实现。

Android控件使用FragmentTabHost,切换Fragment;

大部分APP的主界面都很类似,要么底部导航的,要么就是侧滑菜单,还有底部导航+侧滑菜单的;底部导航实现大概有几种方式: TabHost+Fragment RadioGroup+Fragment FragmentTabHost+Fragment BottomNavigator+Fragment 今天说一下FragmentTabHost+Fragment实现方式(布局文件在最下面): 1、定...

Choi晨的博客 755

Android中使用TabHost 与 Fragment 制作页面切换效果

主要介绍了Android中使用TabHost 与 Fragment 制作页面切换效果的相关资料,需要的朋友可以参考下

PyTorch 深度学习实战(28):对比学习(Contrastive Learning)与自监督表示学习

本文实现了基于对比学习的自监督表征学习框架,通过 InfoNCE 损失在 CIFAR-10 数据集上学习到具有判别性的特征表示。实验表明,仅使用线性分类器即可达到 82% 以上的验证准确率,证明了对比学习的有效性。在下一篇文章中,我们将深入计算机视觉的核心任务之一——《目标检测与 YOLO 实战》,探讨如何利用深度学习实现高效的目标检测系统。

m0_60414444的博客 2393

用FragmentTabHost保存Fragment状态并添加切换动画

本文参考地址:

新的舞台,开始新的梦想!! 2799

数字后端流程--place

Floorplan完成后芯片的大致框架已经出现,万里长征已经迈出特别重要的第一步了,前面的步骤我们已经完成了io port,macro的摆放,现在core外只剩下std cell(标准单元)了,Place这一步就是要把std cell放进core里。看到密密麻麻的std cell也不用紧张,因为放std cell不用像macro手动一个一个摆,工具就可以帮我们完成大部分工作。虽然这一步的工作只有一个,但也不是只要Place完就能进行下一步的,衡量Place质量是否满足标准会通过以下几点判断。

qq_68400574的博客 780

用Fragment+FragmentTabHost++ViewPager 实现底部菜单栏

总体思路图总体设计思路 Fragment:存放不同选项的页面内容 FragmentTabHost:点击切换选项卡 ViewPager:实现页面的左右滑动效果 概念介绍FragmentTabHost用于实现点击选项进行切换选项卡的自定义效果 使用FragmentTabHost,就是先用TabHost“装着”Fragment,然后放进MainActivity里面 ViewPager 定义 ViewP

学渣罗小贱9*w4YBsXS@3W 647

使用everything出现mem_virtual_alloc(): Fatal Error: out of memory解决方案

本文主要介绍了使用everything出现mem_virtual_alloc(): Fatal Error: out of memory解决方案,希望能对使用everything的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 1万+

B07:AI 时代怎么考核团队?我把 KPI 改了 5 版才找到答案

最后说一句掏心窝的话。绩效考核不是"管人"的工具,是"引导团队行为"的工具。你定什么指标,团队就会做什么行为。你定"AI 占比",团队就凑占比你定"效率提升",团队就挑软柿子你定"代码行数",团队就刷行数你定"工具使用率",团队就刷使用只有定"业务价值 + 工程质量 + 能力成长"三个维度的指标,团队才会真正关注长期价值。这套 KPI,我在我带的团队里跑了 6 个月,没有任何一个指标被"刷"出来。考核的是结果,不是过程。永远考核"创造了多少价值",永远不考核"用了多少 AI"。

刘一说的IT专栏 528
上一篇: xUtils的使用总结(草稿未完善)
下一篇: Android应用中去掉标题栏方法总结
playsit
博客等级 码龄11年 2粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值