FragmentTabHost配置文件,布局格式不能改变
<?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" >
<!-- 实际的内容区 -->
<FrameLayout android:id="@+id/fragment_tabhost_realcomment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></FrameLayout>
<!-- FragmentTabHost -->
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- 假的内容区,历史遗留问题 -->
<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>
2、FragmentTabHost操作的步骤:
1)、Activity需要继承自FragmentActivity
android.support.v4.app.FragmentActivity
2)、加载FragmentTabHost布局文件
setContentView(R.layout.activity_fragment_tabhost);
3)、使用findViewById 获得FragmentTabHost 对象
tabHost = (FragmentTabHost)
this.findViewById(android.R.id.tabhost);
4)、使用setup(this, getSupportFragmentManager(),
R.id.realtabcontent)初始化
也就是关联实际的内容区,此处的id是可以自己在布局文件中定义的
5)、使用addTab(newTabSpec()
.setIndicator(),Fragment.class, null)
添加Tab标签与内容
没有过多的布局,只改变了背景颜色),在这里只列出了一个Fragment的布
局文件,和一个java文件
<?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:background="@android:color/holo_purple"
android:orientation="vertical" >
</LinearLayout>
TabHostFragment.java
public class TabHostFragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_fragment, null);
return v;
}
}
MainActivity.java
package com.hngd.fragmenttabhost;
import java.util.zip.Inflater;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class FragmentTabHostActivity extends FragmentActivity {
/*这是将要与TabSpec关联的一个Fragment*/
private Class[] fragments = new Class[]{
TabHostFragment.class,
TabHostFragment2.class,
TabHostFragment3.class,
TabHostFragment4.class
};
private String[] titles = {
"联系人",
"天气",
"股票",
"关于"
};
private FragmentTabHost tabHost;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_fragment_tabhost);
//获取FragmentTabHost
tabHost = (FragmentTabHost)
this.findViewById(android.R.id.tabhost);
//关联实际的内容区-初始化
tabHost.setup(this, getSupportFragmentManager(),
R.id.fragment_tabhost_realcomment);
//创建选项卡标签,每个标签都关联一个Fragment
for(int i = 0; i < fragments.length; i++){
//将TabSpec和Fragment关联
tabHost.addTab(createTabspec(i), fragments[i], null);
//去掉TabSpec之间的分割线
tabHost.getTabWidget().setDividerDrawable(null);
}
}
private TabSpec createTabspec(int index){
//创建标签TabSpec
TabHost.TabSpec tabSpec =
tabHost.newTabSpec(titles[index]);
//给TabSpec设置标题
tabSpec.setIndicator(createTextview(index));
return tabSpec;
}
/**
* 创建TextView对象
* @param index
*/
private View createTextview(int index){
LayoutInflater layoutInflater = LayoutInflater.from(this);
View v = layoutInflater.inflate(R.layout.activity_textview_item, null);
TextView textView = (TextView) v.findViewById(R.id.item_guide);
textView.setText(titles[index]);
return v;
}
}
activity_fragment_tabhost.xml
布局文件说明:
<?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" >
<!-- 实际的内容区 -->
<FrameLayout android:id="@+id/fragment_tabhost_realcomment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></FrameLayout>
<!-- FragmentTabHost -->
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- 假的内容区,历史遗留问题 -->
<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>
2、FragmentTabHost操作的步骤:
1)、Activity需要继承自FragmentActivity
android.support.v4.app.FragmentActivity
2)、加载FragmentTabHost布局文件
setContentView(R.layout.activity_fragment_tabhost);
3)、使用findViewById 获得FragmentTabHost 对象
tabHost = (FragmentTabHost)
this.findViewById(android.R.id.tabhost);
4)、使用setup(this, getSupportFragmentManager(),
R.id.realtabcontent)初始化
也就是关联实际的内容区,此处的id是可以自己在布局文件中定义的
5)、使用addTab(newTabSpec()
.setIndicator(),Fragment.class, null)
添加Tab标签与内容
案例效果图
3、布局和Activity文件
activity_fragment.xml(不同页面切换的Fragment布局文件,为了简单起见文件中没有过多的布局,只改变了背景颜色),在这里只列出了一个Fragment的布
局文件,和一个java文件
<?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:background="@android:color/holo_purple"
android:orientation="vertical" >
</LinearLayout>
TabHostFragment.java
public class TabHostFragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_fragment, null);
return v;
}
}
MainActivity.java
package com.hngd.fragmenttabhost;
import java.util.zip.Inflater;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class FragmentTabHostActivity extends FragmentActivity {
/*这是将要与TabSpec关联的一个Fragment*/
private Class[] fragments = new Class[]{
TabHostFragment.class,
TabHostFragment2.class,
TabHostFragment3.class,
TabHostFragment4.class
};
private String[] titles = {
"联系人",
"天气",
"股票",
"关于"
};
private FragmentTabHost tabHost;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_fragment_tabhost);
//获取FragmentTabHost
tabHost = (FragmentTabHost)
this.findViewById(android.R.id.tabhost);
//关联实际的内容区-初始化
tabHost.setup(this, getSupportFragmentManager(),
R.id.fragment_tabhost_realcomment);
//创建选项卡标签,每个标签都关联一个Fragment
for(int i = 0; i < fragments.length; i++){
//将TabSpec和Fragment关联
tabHost.addTab(createTabspec(i), fragments[i], null);
//去掉TabSpec之间的分割线
tabHost.getTabWidget().setDividerDrawable(null);
}
}
private TabSpec createTabspec(int index){
//创建标签TabSpec
TabHost.TabSpec tabSpec =
tabHost.newTabSpec(titles[index]);
//给TabSpec设置标题
tabSpec.setIndicator(createTextview(index));
return tabSpec;
}
/**
* 创建TextView对象
* @param index
*/
private View createTextview(int index){
LayoutInflater layoutInflater = LayoutInflater.from(this);
View v = layoutInflater.inflate(R.layout.activity_textview_item, null);
TextView textView = (TextView) v.findViewById(R.id.item_guide);
textView.setText(titles[index]);
return v;
}
}