Android中FragmentTabHost控件的使用

Android中FragmentTabHost控件的使用


1、布局文件——activity_fragment_tab_host.xml

<?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"
    tools:context="com.example.tabhostparctice.fragmenttabhost.FragmentTabHostActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:id="@+id/frame_content"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>

    <android.support.v4.app.FragmentTabHost
        android:layout_width="match_parent"
        android:id="@android:id/tabhost"
        android:layout_height="wrap_content">

    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

2、TabHost.TabSpec

  从TabHost控件的常用方法中可以看出,要将Tab加入到TabHost中,需要使用到addTab (TabHost.TabSpec tabSpec)方法,而这个方法的参数是一个TabHost.TabSpec对象,那么TabHost的内部类TabHost.TabSpec是用来干嘛的呢?

  我们已经知道,每个Tab都是由TabWidget和FrameLayout 两部分组成的。而TabHost.TabSpec可以为每个Tab设置一个Tag(类型为String),以此来跟踪每一个Tab。此外,TabHost.TabSpec还可以为Tab设置标签、图标以及内容。由此可以看出TabHost.TabSpec类对TabHost控件来说是及其重要的。

  TabHost.TabSpec类的常用方法如下图所示。

由图可以看出,在TabHost.TabSpec类中提供了设置Tab标签和图标的方法setIndicator(),以及设置Tab内容的方法setContent()。然后我们看一下Activity类中的代码如何进行编写。


3、Activity文件——FragmentTabHostActivity.java

package com.example.tabhostparctice.fragmenttabhost;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.tabhostparctice.R;

public class FragmentTabHostActivity extends FragmentActivity {

    private FragmentTabHost mFragmentTabHost;
    //碎片数组
    private Class<?>[] fragments;
    //Tab文字
    private String[] tabTitle;
    //Tab图片
    private int[] tab_icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_fragment_tab_host);
        initViews();
        initTabs();
    }

    private void initTabs() {
        for (int i = 0; i < fragments.length; i++) {
            TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(tabTitle[i]).setIndicator(getTabItem(i));
            mFragmentTabHost.addTab(tabSpec, fragments[i], null);
            //设置FragmentTabHost的背景色彩
//            mFragmentTabHost.setBackgroundResource(android.R.color.holo_green_light);
            //设置单个TabWidget的背景色彩
            mFragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.fragmenttabhost_bg);
        }
    }

    private void initViews() {
        mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabTitle = new String[] { "TAB——One", "TAB——Two", "TAB——Three" };
        tab_icon = new int[] { R.drawable.b, R.drawable.c, R.drawable.e };
        fragments = new Class[] { FragmentOne.class, FragmentTwo.class, FragmentThree.class };
        mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.frame_content);
    }

    public View getTabItem(int index) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_fragment_indicator, null);
        TextView textivew = (TextView) view.findViewById(R.id.fragment_text_indicator);
        ImageView image = (ImageView) view.findViewById(R.id.fragment_icon_indicator);
        textivew.setText(tabTitle[index]);
        image.setImageResource(tab_icon[index]);
        return view;
    }

}

4、3中Activity文件使用到的Fragment,以及它的布局

(由于三个碎片几乎一模一样,所以这里只贴出一组)

FragmentOne.java

package com.example.tabhostparctice.fragmenttabhost;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.tabhostparctice.R;

/**
 * Created by xiaobaiyang on 2018/4/12.
 */

public class FragmentOne extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_one, container, false);
    }
}

fragment_one.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:text="Fragment——One"
        android:textSize="45dp"/>

</LinearLayout>

运行效果图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值