我们要实现这样最基础的功能
首先创建主活动和注册,登录活动
在main活动里面的布局代码
<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"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
这里注意
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
android:layout_height="wrap_content"
这句话不要写成
android:layout_width="match_parent"
main活动
package com.example.tabhostdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
public class MainActivity extends TabActivity {
TabHost tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab=getTabHost();
Intent denglu=new Intent(this,Denglu.class);
tab.addTab(tab.newTabSpec("tab1").setIndicator("登录").setContent(denglu));
Intent zhuce=new Intent(this,Zhuce.class);
tab.addTab(tab.newTabSpec("tab2").setIndicator("注册").setContent(zhuce));
/*
* tab.setCurrentTab(1);
* 可以用这种方法设置默认的选项(从0开始)
* */
tab.setCurrentTabByTag("tab2");//和上面一样的效果
//设置选项卡改变监听
tab.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
// TODO 自动生成的方法存根
Toast.makeText(MainActivity.this, "当前是"+arg0+"界面", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
登录界面和注册界面放置一个TextView 就行。主要是实现功能
Tip: 想把选项卡放置到最后,在main函数的布局文件改一下就行
<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"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TabWidget>
</LinearLayout>
</TabHost>
就是把FramdeLayout和TabWidget交换一下位置,然后把FrameLayout里面的Layou_weight设置为1