Android笔记之FragmentTabHost实现选项卡

时间:2021-12-04 06:26:14

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!