Android笔记之FragmentTabHost实现选项卡

FragmentTabHost

API:http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

1、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" />

    <!-- tabhost装载5个tab的容器 -->
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

2、FragmentActivity中使用fragmenttabhost

(1)TabSpec的实例化:TabSpec tabSpec = mTabHost.newTabSpec(String tag).setIndicator(.....);

  • setIndicator(View view);   //Specify a view as the tab indicator.
setIndicator(getTabItemView())
private View getTabItemView(int index){
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);
    
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(R.drawable.tab_home_btn);
        
        TextView textView = (TextView) view.findViewById(R.id.textview);        
        textView.setText("首页");
    
        return view;
    }
  • setIndicator(CharSequence label, Drawable icon)  //Specify a label and icon as the tab indicator. label即为选项卡的显示的文本
setIndicator("Android",getResources().getDrawable(R.drawable.icon_home_nor));       
  • setIndicator( CharSequence label)  //Specify a label as the tab indicator.
setIndicator("设置")

主要代码:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

/**
 * This demonstrates how you can implement switching between the tabs of a
 * TabHost through fragments, using FragmentTabHost.
 */
public class MainActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); mTabHost.addTab(mTabHost.newTabSpec("首页").setIndicator("Simple"), fragment_1.class, null); mTabHost.addTab(mTabHost.newTabSpec("分类") .setIndicator("Contacts"), fragment_1.class, null); mTabHost.addTab(mTabHost.newTabSpec("排行").setIndicator("Custom"), fragment_1.class, null); mTabHost.addTab(mTabHost.newTabSpec("热门") .setIndicator("Throttle"), fragment_1.class, null); mTabHost.addTab(mTabHost.newTabSpec("设置") .setIndicator("Throttle"), fragment_1.class, null); } }
fragment_X.class的代码
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragment_1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_1, container, false);
        return v;
    }
}

3、fragment下使用fragmentabhost

 (1)fragment_1本身不需要布局文件

(2)选项卡默认在顶部

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;public class fragment_1 extends Fragment {
    private FragmentTabHost mTabHost;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment1/59);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                subfragment_1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                subfragment_1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                subfragment_1.class, null);
        return mTabHost;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabHost = null;
    }
}

 

 

4、其他函数

mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);

Done!

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/xingyyy/p/3322011.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值