目前许多应用类的APP在首页下方有tab栏,例如新浪微博等等,而这个下端的工具栏是由tabHost实现的,下面在这里总结一下用法。
首先要想使用tabHost,必须先导一个v7的jar包到当前的目录中。
然后要布局一个xml文件来实现tabHost的显示位置。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/realcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="50dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
然后布局一下tabHost里的每个tab的布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@android:color/black">
<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:drawablePadding="2dp"
android:textColor="@android:color/white"
android:drawableTop="@drawable/ic_nearby_normal"/>
</RelativeLayout>
一般在一个页面中,tabHost的上面是一个frameLayout来显示每个tab对应的页面内容。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
每个页面对应的是一个Fragment。所以我们要自定义一个Fragment并且让它继承Fragment,在这个文件中把要显示的东西定义好,然后让它把内容显示在定义的framlayout的xml文件中。
public class NewsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_layout, null);
TextView text=(TextView)view.findViewById(R.id.text);
//接收Acitivity传递过来的数据
Bundle bundle=getArguments();
text.setText(bundle.getString("title"));
return view;
}
}
最后在主Activity里面,对tabHost里的每个tab进行赋值初始化,然后每个tab对应的每个fragment内容显示出来即可
public class MainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
private String title[]={"首页","精选","榜单","我"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablehost_main);
tabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(getApplicationContext(), getSupportFragmentManager(), R.id.realcontent);
for(int i=0;i<4;i++){
TabSpec tab=tabHost.newTabSpec("f"+i);
View view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_item, null);
TextView textView=(TextView)view.findViewById(R.id.msg);
textView.setText(title[i]);
tab.setIndicator(view);
Bundle bundle=new Bundle();
bundle.putString("title", title[i]);
tabHost.addTab(tab, NewsFragment.class, bundle);
}
}
}