android 自定义TabActivity的实例方法
一、改变tab栏的位置。
java代码。在tabactivity的oncreate方法中添加
setcontentview(r.layout.tab_host);
其中 layout tab_host.xml 是从系统资源文件中抠出来之后略作修改。
系统原来的 tab_host.xml内容如下
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/layout/tab_content.xml
**
** copyright 2006, the android open source project
**
** licensed under the apache license, version 2.0 (the "license");
** you may not use this file except in compliance with the license.
** you may obtain a copy of the license at
**
** http://www.apache.org/licenses/license-2.0
**
** unless required by applicable law or agreed to in writing, software
** distributed under the license is distributed on an "as is" basis,
** without warranties or conditions of any kind, either express or implied.
** see the license for the specific language governing permissions and
** limitations under the license.
*/
-->
<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">
<linearlayout android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<tabwidget android:id="@android:id/tabs" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="0" />
<framelayout android:id="@android:id/tabcontent"
android:layout_width="match_parent" android:layout_height="0dip"
android:layout_weight="1"/>
</linearlayout>
</tabhost>
要实现tab栏在页面下方,只需简单修改。
<?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">
<framelayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="0dip"
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>
这样,就实现了tab栏在页面下册。需要注意的是,view的id不要修改。
二、自定义tab的图片。系统自带的tab_indicator.xml内容如下
<?xml version="1.0" encoding="utf-8"?>
<!-- copyright (c) 2008 the android open source project
licensed under the apache license, version 2.0 (the "license");
you may not use this file except in compliance with the license.
you may obtain a copy of the license at
http://www.apache.org/licenses/license-2.0
unless required by applicable law or agreed to in writing, software
distributed under the license is distributed on an "as is" basis,
without warranties or conditions of any kind, either express or implied.
see the license for the specific language governing permissions and
limitations under the license.
-->
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight="1"
android:layout_marginleft="-3dip"
android:layout_marginright="-3dip"
android:orientation="vertical"
android:background="@android:drawable/tab_indicator">
<imageview android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerhorizontal="true"
/>
<textview android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentbottom="true"
android:layout_centerhorizontal="true"
style="?android:attr/tabwidgetstyle"
/>
</relativelayout>
可以看出,默认情况下,图标在文字上方,并且不能占到整个格,无法满足设计需要。因此可以重写该layout。
编写tab_in.xml
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="64dip"
android:orientation="vertical"
>
</relativelayout>
view view1 = inflater.inflate(r.layout.tab_in, null);;
view view2 = inflater.inflate(r.layout.tab_in, null);;
view view3 = inflater.inflate(r.layout.tab_in, null);;
view1 .setbackgroundresource(r.drawable.record_upload_button_stateful);
view2 .setbackgroundresource(r.drawable.record_download_button_stateful);
view3 .setbackgroundresource(r.drawable.record_receive_button_stateful);
tabhost.addtab(tabhost
.newtabspec("view1")
.setindicator(view1)
);
tabhost.addtab(tabhost
.newtabspec("view2")
.setindicator(view2)
);
tabhost.addtab(tabhost
.newtabspec("view3")
.setindicator(view3)
);
推荐阅读
-
android在异步任务中关闭Cursor的代码方法
-
android TabHost(选项卡)的使用方法
-
android 自定义TabActivity的实例方法
-
android通过配置文件设置应用安装到SD卡上的方法
-
Android7.0开发实现Launcher3去掉应用抽屉的方法详解
-
Yii框架实现邮箱激活的方法【数字签名】_php实例
-
php中XMLHttpRequestAjax不能设置自定义的Referer的解决方法
-
Jquery获取iframe的各级元素、内容或者ID的实例方法详解
-
thinkPHP实现递归循环栏目并按照树形结构无限极输出的方法_php实例
-
android上传图片到PHP的过程详解_php实例