在Titanium开发Android应用时实现底部tabbar

[size=medium]在Android应用中,默认是把Tabbar放在顶部的,但是我们经常看到有些应用模范iPhone应用将Tabbar实现到底部去,那么在Titanium中我们是否也能实现将Tabbar放到底部呢?答案当然是能。在Titanium中TabGroup就是Android的Tabbar。[/size]

[size=medium]首先我们先创建一个Titanium项目,默认就是一个带了TabGroup的demo项目了。接下来要实现以上效果,其实也很简单,只需在你的项目根目录里添加一个android的xml布局文件就可以了,但是这个xml文件必须命名为:[/size]

[color=red][b][size=medium]titanium_tabgroup.xml[/size][/b][/color]


<?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"
android:padding="0dp">

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"/>

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>

</LinearLayout>

</TabHost>

[size=medium]接下来就是要在你的项目根目录下创建以下路径和目录:
/platform/android/res/layout/[/size]

[size=medium]创建完的项目结构如下:[/size]
[img]http://dl.iteye.com/upload/attachment/0064/4717/f04f152e-b2c3-3ebd-9717-c71e873712f5.png[/img]

[size=medium]然后再重新clean后再次运行,效果如下:[/size]
[img]http://dl.iteye.com/upload/attachment/0065/9311/b1171a3c-a9e6-34da-9363-a42370391dcf.gif[/img]

[color=blue][b][size=medium]其实这中间的道理很简单,就是用我们的布局文件覆盖了Titanium既存的布局文件,让应用使用我们的布局文件来运行代码。

关于这里为什么一定要命名为titanium_tabgroup.xml,可以参看源代码的[url=https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/ui/src/java/ti/modules/titanium/ui/TiTabActivity.java]TiTabActivity.java[/url]74行-77行
[/size][/b][/color]

int layoutResId = getResources().getIdentifier("titanium_tabgroup", "layout", getPackageName());
if (layoutResId == 0) {
throw new IllegalStateException("titanium_tabgroup layout resource not found. TabGroup cannot be created.");
}


[color=red][size=large][b]补充:

可以延伸一下,有很多朋友在做应用的时候不想显示Tabbar,将titanium_tabgroup.xml变通一下就可以实现。将 TabWidget设置为android:layout_height="0dp"。[/b][/size][/color]

<?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"
android:padding="0dp">

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_weight="1"/>

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0"/>

</LinearLayout>

</TabHost>


[size=medium]再重新clean后再次运行,效果如下:[/size]
[img]http://dl.iteye.com/upload/attachment/0065/9313/e64ddfa9-27bf-3d2a-8ee2-3d9de797a4f2.gif[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值