FragmentTabHost的使用(上面切换)

FragmentTabHost的使用(上面切换)

效果:这是显示在上面
     
布局: 
     
   xmlns: tools = "http://schemas.android.com/tools"
   
android :layout_width= "match_parent"
   
android :layout_height= "match_parent"

   
tools :context= ".activity.MainActivity" >

    <android.support.v4.app.FragmentTabHost
       
android :id= "@+id/tab"
       
android :layout_width= "match_parent"
       
android :layout_height= "match_parent"
       
>
   
<!-- 以下的布局是固定写法
         android:id="@android:id/tabs 
表示指向安卓的某个 ID">-->
   
<LinearLayout
       
android :layout_width= "match_parent"
       
android :layout_height= "match_parent"
       
android :orientation= "vertical"
       
>
       
<!-- 用于切换的 view-->
       
<TabWidget
           
android :layout_width= "match_parent"
           
android :layout_height= "wrap_content"
           
android :id= "@android:id/tabs" >
        </TabWidget>

       
<!-- 每切换一次显示的 Fragment-->
       
<FrameLayout
            
android :layout_width= "match_parent"
            
android :layout_height= "match_parent"
            
android :id= "@android:id/tabcontent"
           
></FrameLayout>
    </LinearLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

FragmentClock布局:
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
             
android :orientation= "horizontal"
             
android :layout_width= "match_parent"
             
android :layout_height= "match_parent"
             
android :gravity= "center" >
    <TextView
       
android :layout_width= "wrap_content"
       
android :layout_height= "wrap_content"
       
android :text= "clock"
       
android :id= "@+id/clock"
       
android :textColor= "@android:color/black"
       
android :textSize= "22sp"

       
/>

</LinearLayout>


Mainactivity代码
public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate (Bundle savedInstanceState) {
        super .onCreate(savedInstanceState) ;
        setContentView(R.layout. activity_main)
        FragmentTabHost tab = (FragmentTabHost) findViewById(R.id. tab ) ;

       
// 第三个参数 android.R.id.tabcontent 是要放的内容的控件 ID ,也就是 FragmentLayout
        //
这个是初始化,必须要先初始化
       
tab.setup( this, getSupportFragmentManager() , android.R.id. tabcontent ) ;

        // 第一个参数设置 每一个选项卡 外观,第二个是设置要跳到哪一个页面
        tab.addTab(tab.newTabSpec( "1" ).setIndicator( "one" ) , FragmentClock. class,null ) ;
       
tab.addTab(tab.newTabSpec( "2" ).setIndicator( "two" ) , FragmentClock. class,null ) ;
       
tab.addTab(tab.newTabSpec( "3" ).setIndicator( "three" ) , FragmentClock. class,null ) ;
       
tab.addTab(tab.newTabSpec( "4" ).setIndicator( "four" ) , FragmentClock. class,null ) ;
       
tab.addTab(tab.newTabSpec( "5" ).setIndicator( "five" ) , FragmentClock. class,null ) ;

    }

FragmentClock 代码
public class FragmentClock extends Fragment {

   
@Nullable
    @Override
   
public View onCreateView (LayoutInflater inflater , @Nullable ViewGroup container , @Nullable Bundle savedInstanceState) {

       
return inflater.inflate(R.layout. fragment_clock ,null ) ;
   
}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值