TabLayout的用法

例子:
TabAcitvity.class
public class TabActivity extends android.app.TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTabFrame(FrameActivity.class);
        setTabFrame(Frame2Activity.class);
    }
    private void setTabFrame(Class ClassName){
    	//得到TabHost对象。
		TabHost tab = getTabHost();
		//生成一个Intent对象,该对象指向一个Activity
		Intent i = new Intent();
		i.setClass(this, ClassName);
		//生成一个TabSpec对象。这个对象代表一个页
		TabHost.TabSpec remote = tab.newTabSpec("Remote");
		//设置一个图片和文字
		Resources res = getResources();
		remote.setIndicator("Remote", res.getDrawable(android.R.drawable.stat_sys_download));
		remote.setContent(i);
		tab.addTab(remote);
    }
}

FrameActivity.class
public class FrameActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frame1);
	}
}

Frame2Activity.class
public class Frame2Activity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frame2);
	}
}

对应TabActivity的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent">
        <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
            <TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
            <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.cn"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TabActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<activity android:name=".FrameActivity" android:label="@string/app_name"></activity>
		<activity android:name=".Frame2Activity" android:label="@string/app_name"></activity>
    </application>
</manifest>


PS:Frame和frame2的layout布局xml不需要做特殊处理,填写本身需要显示的内容既可

你可能感兴趣的:(layout)