[android]使用TabHost+Fragment(替… 分类: Android开发 2014-05-30 10:57 108人阅读 评论(0) 收藏

MainActivity
[java] 
package com.demo.tabfragmenttest; 
 
import android.R.anim; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TabHost; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.NavUtils; 
 
public class MainActivity extends FragmentActivity { 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
        tabHost.setup(); 
         
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1)); 
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.tab2)); 
   
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        return true; 
   
 
     


MainActivity对应的layout-activity_main.xml:
[java
http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" > 
 
            android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical" > 
 
                    android:id="@android:id/tabs" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" > 
         
 
                    android:id="@android:id/tabcontent" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" > 
             
           
                android:id="@+id/tab1" 
                android:layout_width="match_parent" 
                android:layout_height="match_parent"/> 
             
           
                android:id="@+id/tab2" 
                android:layout_width="match_parent" 
                android:layout_height="match_parent"/> 
        
     
 

 

当然,另外你需要建立两个Fragment:Tab1Fragment及Tab2Fragment,这里就省略

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-05-30 10:57  leansmall  阅读(69)  评论(0编辑  收藏  举报