android FragmentTabhost实现导航分页

本文实例为大家分享了android FragmentTabhost导航分页展示的具体代码,供大家参考,具体内容如下

基本模板

public class MainActivity extends FragmentActivity {

  private FragmentTabHost mTabHost;
  private LayoutInflater mLayoutInflater;

  private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
      Fragment3.class, Fragment4.class, Fragment5.class };

  private int mImageArray[] = { R.drawable.tab_home_btn,
      R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
      R.drawable.tab_square_btn, R.drawable.tab_more_btn };

  private String mTextArray[] = { "首页", "消息", "好友", "搜索", "更多" };

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
  }

  private void initView() {

    mLayoutInflater = LayoutInflater.from(this);

    // 找到TabHost
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
     mTabHost.getTabWidget().setDividerDrawable(null);//去除分割线
    // 得到fragment的个数
    for (int i = 0; i < mFragmentArray.length; i++) {
      // 给每个Tab按钮设置图标、文字和内容
      TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
          .setIndicator(getTabItemView(i));
      // 将Tab按钮添加进Tab选项卡中
      mTabHost.addTab(tabSpec, mFragmentArray[i], null);
      // 设置Tab按钮的背景
      mTabHost.getTabWidget().getChildAt(i)
          .setBackgroundResource(R.drawable.selector_tab_background);
    }
  }

  //给每个Tab按钮设置图标和文字
  private View getTabItemView(int index) {
    View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
    ImageView imageView = view.findViewById(R.id.imageview);
    //设置图片选择器,选中的tab改变图标
    switch (index){
      case 0:imageView.setImageResource(R.drawable.main_bottom_image_selector);break;
      case 1:imageView.setImageResource(R.drawable.main_bottom_image_selector2);break;
      case 2:imageView.setImageResource(R.drawable.main_bottom_image_selector3);break;
      case 3:imageView.setImageResource(R.drawable.main_bottom_image_selector4);break;
      case 4:imageView.setImageResource(R.drawable.main_bottom_image_selector5);break;
    }
    TextView textView = view.findViewById(R.id.textview);
    textView.setText(mTextArray[index]);
    //设置文本选择器,选中的tab文字高亮
    textView.setTextColor(R.drawable.main_bottom_text_selector);
    return view;
  }

}

acitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <FrameLayout
  android:id="@+id/realtabcontent"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1" />

 <android.support.v4.app.FragmentTabHost
  android:id="@android:id/tabhost" //必须使用提供的id
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/bg_tabhost_bg">

  <FrameLayout
   android:id="@android:id/tabcontent" //必须使用提供的id
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:layout_weight="0" />
 </android.support.v4.app.FragmentTabHost>

</LinearLayout>

tab_item_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:orientation="vertical">
  <ImageView
    android:id="@+id/imageview"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_margin="3dp"
    />
  <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:textSize="12sp"
    android:layout_marginBottom="2dp"/>
</LinearLayout>

main_bottom_image_selector图片选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="false" android:drawable="@drawable/ic_launcher" />
  <item android:state_selected="true" android:drawable="@drawable/ic_launcher_round" />
</selector>

注:模板有5个tab,需要5个图片选择器,还需要5个文本选择器,还需要创建5个fragment。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android 利用ViewPager+GridView实现首页导航栏布局分页效果

    最近我尝试使用ViewPager+GridView实现的,看起来一切正常,废话不多说,具体代码如下: 如图是效果图 首先分析下思路 1.首先是怎么布局:整体是一个ViewPager将GridView作为一个View添加到ViewPager的adapter中,下方是圆点 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.a

  • Android分页中显示出下面翻页的导航栏的布局实例代码

    当页面条目过多的时候需要分页,要在布局中显示出分页的相关布局,使用android:layout_weight="11" activity_call_safe.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:lay

  • Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)

    采取的方法是Fragment+FragmentTabHost组件来实现这种常见的app主页面的效果 首先给出main.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

  • Android 使用FragmentTabHost实现底部菜单功能

    前言 现在大部分App底部都有一个菜单,实现这个功能也有好多办法: - TabHost+Fragment - RadioGroup+Fragment - FragmentTabHost+Fragment - 5.0以后有个新控件,BottomNavigator 这篇主要介绍下FragmentTabHost配合Fragment使用 运行效果图 效果图分析 FragmentTabHost简单介绍 首先我们看下官方文档的介绍 OK,接着官方还给出了一份使用的代码,我们也来看下 Activity中使用

  • android FragmentTabhost实现导航分页

    本文实例为大家分享了android FragmentTabhost导航分页展示的具体代码,供大家参考,具体内容如下 基本模板 public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost; private LayoutInflater mLayoutInflater; private Class mFragmentArray[] = { Fragment1.class, Fragment

  • Android实现底部导航栏功能(选项卡)

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其中各个类的作用以及资源文件就不详细解释了,还有资源图片(在该Demo中借用了其它应用程序的资源图片)也不提供了,大家可以自行更换自己需要的资源图片.直接上各个布局文件或各个类的代码: 1. res/layout目录下的 maintabs.xml 源码: <?xml version="1.0&q

  • Android百度定位导航之基于百度地图移动获取位置和自动定位

    一.问题描述 使用百度地图实现如图所示应用,首先自动定位当前我起始位置(小圆点位置),并跟随移动不断自动定位我的当前位置 百度Api不同版本使用会有些差异,本例中加入lib如下: 二.编写MyApplication类 public class MyApplication extends Application { static MyApplication myApplication; BMapManager mBMapManager = null; String mStrKey = "7ZfuR

  • Android开发中滑动分页功能实例详解

    本文实例讲述了Android开发中滑动分页功能.分享给大家供大家参考,具体如下: android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: Java代码: package cn.anycall.ju; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import andro

  • Android 状态栏虚拟导航键透明效果的实现方法

    状态栏和虚拟导航键 4.4上半透明,5.0以上可以全透明 先上效果 4.4 半透明效果 5.0及以上 全透明效果 上代码 MainActivity代码 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 隐藏标题栏 supportReque

  • Android提高之SQLite分页表格实现方法

    继前一篇文章讲到Android上的SQLite分页读取,其功能只是用文本框显示数据而已.本文就讲得更加深入些,实现并封装一个SQL分页表格控件,不仅支持分页还是以表格的形式展示数据. 先来看看本文程序运行的动画如下图所示: 这个SQL分页表格控件主要分为"表格区"和"分页栏"这两部分,这两部分都是基于GridView实现的.网上介绍Android上实现表格的DEMO一般都用ListView.ListView与GridView对比,ListView最大的优势是格单元的

  • Android实现顶部导航栏可点击可滑动效果(仿微信仿豆瓣网)

    使用ViewPager,PagerSlidingTabStrip,SwipeRefreshLayout打造一款可以点击可以侧滑的顶部导航栏. 先简单介绍一下所用的两个个开源库. PagerSlidingTabStrip Github地址 用法: 1.向app Module中的build.gradle中添加依赖 dependencies { compile 'com.astuetz:pagerslidingtabstrip:1.0.1' } 2.把PagerSlidingTabStrip这个控件添

  • android SectorMenuView底部导航扇形菜单的实现代码

    这次分析一个扇形菜单展开的自定义View, 也是我实习期间做的一个印象比较深刻的自定义View, 前后切换了很多种实现思路, 先看看效果展示 效果展示 效果分析 点击圆形的FloatActionBar, 自身旋转一定的角度 菜单像波纹一样扩散开来 显示我们添加的item 实现分析 使用adapter适配器去设置View, 用户可自定义性强, 不过每次使用需要去设置Adapter, 较为繁琐 直接调用ItemView, 将ImageView和TextView写死, 用户操作简单, 但是缺乏可定制性

  • Android实现底部导航栏功能

    本文实例为大家分享了Android实现底部导航栏功能的具体代码,供大家参考,具体内容如下 实验效果: (1)在drawable文件夹下新建tab_menu_bg.xml文件,具体代码如下: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item

  • bootstrap 路径导航 分页 进度条的实例代码

    路径导航 <ol class="breadcrumb"> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel=&

随机推荐