Tab and TabHost

<span style="font-family: verdana, helvetica, arial, sans-serif; color: #444444;"><span style="border-collapse: collapse; line-height: 22px; font-size: small;">[b]创建tab时,继承的是tabactivity而不是[/b]</span></span><span style="font-family: verdana, helvetica, arial, sans-serif; font-size: small; border-collapse: collapse; color: #444444; line-height: 22px;">[b]activity。[/b]</span>
 
public class tabs extends tabactivity{}
[b][size=small;]<span style="font-family: verdana, helvetica, arial, sans-serif; font-size: 14px; border-collapse: collapse; color: #444444; line-height: 22px;">盛放tab的容器就是tabhost。[/size]  </span>[/b]
[b]</strong><span style="">[b][size=small;]然后需要在layout里,修改布局方式为framelayout。这种布局方式将允许有子布局的存在,这就对应了tabactivity的布局方式。在tabhost内,每个将会存在的页签都对应了framelayout下的一个具体layout[/size][/b]。</span>
 
 
 
<?xml version="1.0" encoding="utf-8"?><framelayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent" android:layout_height="fill_parent">    <linearlayout android:id="@+id/widget_layout_blue"        android:layout_width="fill_parent" android:layout_height="fill_parent"        androidrientation="vertical" >        <edittext android:id="@+id/widget34" android:layout_width="fill_parent"            android:layout_height="wrap_content" android:text="edittext"            android:textsize="18sp">        </edittext>        <button android:id="@+id/widget30" android:layout_width="wrap_content"            android:layout_height="wrap_content" android:text="button">        </button>    </linearlayout>    <linearlayout android:id="@+id/widget_layout_red"        android:layout_width="fill_parent" android:layout_height="fill_parent"        androidrientation="vertical"  >        <analogclock android:id="@+id/widget36"            android:layout_width="wrap_content" android:layout_height="wrap_content">        </analogclock>    </linearlayout>    <linearlayout android:id="@+id/widget_layout_green"        android:layout_width="fill_parent" android:layout_height="fill_parent"        androidrientation="vertical">        <radiogroup android:id="@+id/widget43"            android:layout_width="166px" android:layout_height="98px"            androidrientation="vertical">            <radiobutton android:id="@+id/widget44"                android:layout_width="wrap_content" android:layout_height="wrap_content"                android:text="radiobutton">            </radiobutton>            <radiobutton android:id="@+id/widget45"                android:layout_width="wrap_content" android:layout_height="wrap_content"                android:text="radiobutton">            </radiobutton>        </radiogroup>    </linearlayout></framelayout>
 
[b][size=small;] [/size][/b]<span style="">[b][size=small;]通过代码即可将layout分布到各个tab上面去。[/size][/b]</span>
 
 
private tabhost mytabhost;	@override	protected void oncreate(bundle bundle){		super.oncreate(bundle);		mytabhost=this.gettabhost();//从tabactivity上面获取放置tab的tabhost		layoutinflater.from(this).inflate(r.layout.tabs, mytabhost.gettabcontentview(), true);		//from(this)从这个tabactivity获取layoutinflater		//r.layout.main 存放tab布局		//通过tabhost获得存放tab标签页内容的framelayout		//是否将inflate 拴系到根布局元素上		mytabhost.setbackgroundcolor(color.argb(150, 22, 70, 150));		//设置一下tabhost的颜色		mytabhost        .addtab(mytabhost.newtabspec("k1")// 制造一个新的标签k1                .setindicator("kk",                        getresources().getdrawable(r.drawable.a))                // 设置一下显示的标题为kk,设置一下标签图标为a                .setcontent(r.id.widget_layout_red));		mytabhost.setontabchangedlistener(new ontabchangelistener(){            public void ontabchanged(string tabid) {                            }                    });				mytabhost        .addtab(mytabhost.newtabspec("k2")// 制造一个新的标签k2                .setindicator("kk",                        getresources().getdrawable(r.drawable.b))                // 设置一下显示的标题为kk,设置一下标签图标为b                .setcontent(r.id.widget_layout_green));		mytabhost.setontabchangedlistener(new ontabchangelistener(){            public void ontabchanged(string tabid) {                            }                    });				mytabhost        .addtab(mytabhost.newtabspec("k3")// 制造一个新的标签k3                .setindicator("kk",                        getresources().getdrawable(r.drawable.c))                // 设置一下显示的标题为kk,设置一下标签图标为c                .setcontent(r.id.widget_layout_blue));		mytabhost.setontabchangedlistener(new ontabchangelistener(){            public void ontabchanged(string tabid) {                            }                    });			}
[b][size=small;] [/size][/b]<span style="">[b][size=small;]设置了一个ontabchangedlistener,用于监听当页签选择改变的事件.[/size][/b]</span>
	mytabhost.setontabchangedlistener(new ontabchangelistener(){            public void ontabchanged(string tabid) {                toast.maketext(getapplicationcontext(),"点击了tab"+tabid, toast.length_short).show();            }  
 <br><img src="http://dl.iteye.com/upload/attachment/266017/e4361a65-7063-3d29-8162-00262197b19d.jpg" alt=""><br> <span style="">[size=small;]<strong>页签的显示就完成了,下面就需要实现与页签对应的不同菜单。[/b][/size]</span>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值