android Fragment之tabhost

1 : 布局文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <fragment
                android:name="cn.sh.sis.contract.activity.ContractCallLogFragment"
                android:id="@+id/tabcalllog"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <fragment
                android:name="cn.sh.sis.contract.activity.ContractUserFragment"
                android:id="@+id/tabuser"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
  </FrameLayout>
  
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_marginTop="48px"
            android:layout_height="61px"/>
 
    </RelativeLayout>
</TabHost>

2 activity中调用(Class A extends FragmentActivity):

private void initView() {
  mTabHost = (TabHost) findViewById(android.R.id.tabhost);
  mTabHost.setup();
  mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
  LinearLayout tabRecently = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.tab_recently,mTabWidget,false);
  LinearLayout tabPerson = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.tab_person,mTabWidget,false);
  mTabHost.addTab(mTabHost.newTabSpec(TAB_CALL_LOG).setIndicator(tabRecently).setContent(R.id.tabcalllog));
  mTabHost.addTab(mTabHost.newTabSpec(TAB_USER_LOG).setIndicator(tabPerson).setContent(R.id.tabuser));
  mTabHost.setCurrentTab(0);
 }

3:TabWidget(R.layout.tab_recently) 下对应的布局文件

   1):

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

</LinearLayout>

   2):android:background下 :

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" >  <item android:state_focused="false"      android:state_selected="true"      android:state_pressed="false"      android:drawable="@drawable/btn_recently_person_press"  />   <item android:state_focused="false"      android:state_selected="false"      android:state_pressed="false"      android:drawable="@drawable/btn_recently_person_normal" /> 

</selector>

 

 

ok 这就实现 Fragment 与tabhost结合使用的效果了

 

 

posted on 2013-05-10 17:45  yujian_bcq  阅读(272)  评论(0编辑  收藏  举报

导航