android TabHost(选项卡)的使用方法
全能侍卫,外交学考研,五金店转让
首先,定义tabhost的布局文件:
<?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="fill_parent"
android:layout_height="fill_parent">
<linearlayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<tabwidget android:id="@android:id/tabs"
android:layout_alignparentbottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<framelayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</linearlayout>
</tabhost>
其中,tabwidget即是选项卡上面的标签,framelayout是选项卡的内容。
在java类文件中定义如下:
public class mainactivity extends tabactivity {
private tabhost my_tabhost;
private tabwidget my_tabwidget;
private int i,k;
private textview tv;
private string[] tabmenu = { "系统", "硬件", "操作"};
private intent intent0, intent1, intent2;
private intent[] intents = { intent0, intent1, intent2};
private tabhost.tabspec tabspec0, tabspec1, tabspec2, tabspec3;
private tabhost.tabspec[] tabspecs = { tabspec0, tabspec1, tabspec2, tabspec3};
public static context mcontext;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
// 不要窗体标题
requestwindowfeature(window.feature_no_title);
setcontentview(r.layout.activity_main);
setcontentview(r.layout.activity_main);
my_tabhost = gettabhost();
intent0 = new intent(this, system.class);
intent1 = new intent(this, hardware.class);
intent2 = new intent(this, operation.class);
tabspec0 = my_tabhost.newtabspec("system").setindicator(tabmenu[0],null).
setcontent(intent0);
tabspec1 = my_tabhost.newtabspec("hardware").setindicator(tabmenu[1],null).
setcontent(intent1);
tabspec2 = my_tabhost.newtabspec("operation").setindicator(tabmenu[2],null).
setcontent(intent2);
my_tabhost.addtab(tabspec1);
my_tabhost.addtab(tabspec0);
my_tabhost.addtab(tabspec2);
<br> // 设置默认选中的选项卡为第2个
my_tabhost.setcurrenttab(1);
}
}
每一个选项卡对应一个intent,每一个intent又对应一个类,选中这个选项卡时,就显示对应的类。
运行结果如下:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
网友评论