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)
             );

(0)

相关推荐

  • Android实现Activity界面切换添加动画特效的方法

    本文以实例形式展示了Android实现Activity界面切换添加动画特效的方法,对于Android程序设计人员来说有很好的参考借鉴价值.具体方法如下: 了解Android程序设计的人应该知道,在Android 2.0之后有了overridePendingTransition(),其中里面两个参数,一个是前一个activity的退出,另一个activity的进入. 现看看下面这段示例代码: @Override public void onCreate(Bundle savedInstanceSt

  • Android基础之Fragment与Activity交互详解

    今天继续讲解Fragment组件的特性,主要是跟Activity的交互和生命周期的关系,我们前面已经说过Fragment是依赖于Activity的,而且生命周期也跟Activity绑定一起.下面我们看看Fragment跟Activity的关系. 1.为Activity创建事件回调方法在一些情况下, 你可能需要一个fragment与activity分享事件. 一个好的方法是在fragment中定义一个回调的interface, 并要求宿主activity实现它.当activity通过interfa

  • Android实现可使用自定义透明Dialog样式的Activity完整实例

    本文实例讲述了Android实现可使用自定义透明Dialog样式的Activity.分享给大家供大家参考,具体如下: 有时你需要一个对话框,但同时对话框中的内容有更多控制和能控制其生命周期,这时你可以使用带有Dialog样式的Activity来应用你的项目中,想使Activity有对话框那样效果可以在Androidmanifest中添加 Android:style/Theme.Dialog 的主题特性 例如这样: <activity android:name="MyDialogActivi

  • Android使用Theme自定义Activity进入退出动画的方法

    本文实例讲述了Android使用Theme自定义Activity进入退出动画的方法.分享给大家供大家参考,具体如下: 有没有觉得Activity的默认动画太快了或者太难看了.. 我原来使用Activity.overridePendingTransition来自定义Activity的进入动画,却发现没法定义退出的动画.结果就发现了强大的Theme和Style,之后还需要好好研究一下. 具体是这样子的: 在AndroidManifest里面,对于application和activity标签可以定义t

  • Android Activity之间传递图片(Bitmap)的方法

    在Android开发中:Activity之间传递参数是常见的事:如果我们要在Activity之间传递图片:1.MainActivity中包括一个ImageView:当我们点击ImageView时:把图片传递给另外一个Activity MainActivity的主要代码: 复制代码 代码如下: Intent intent=new Intent(MainActivity.this,TranActivity.class);            intent.putExtra("bitmap"

  • Android 多个Activity之间的传值

    下面是主Activity的代码: 开发:Activity之间的传值 - 51CTO.COM 复制代码 代码如下: package com.chaoyang.activity; import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.text.style.BulletSpan;import android.view.View;import android.wi

  • Android的Activity跳转动画各种效果整理

    大家使用Android的原生UI都知道,Android的Activity跳转就是很生硬的切换界面.其实Android的Activity跳转可以设置各种动画.下面给大家看看效果:  实现非常简单,用overridePendingtransition(int inId, int outId)即可实现.inId是下一界面进入效果的xml文件的id,outId是当前界面退出效果的xml文件id. 效果是用xml文件写的,首先要在res文件夹下建立anim文件夹,然后把动画效果xml文件放到里面去. 下面

  • Android实现将一个Activity设置成窗口样式的方法

    本文实例讲述了Android实现将一个Activity设置成窗口样式的方法.分享给大家供大家参考,具体如下: 1.在res/value文件夹下的style.xml文件中加入如下代码: <style name="Theme.FloatActivity" parent="android:style/Theme.Dialog"> <!-- float_box为我们定义的窗口背景 ,这个不是必须的--> <item name="and

  • android的activity跳转到另一个activity

    开发环境:android4.1.1 实验功能:在第一个Hello World!为标签的activity中显示good,该界面中有一个名为Next的按钮.点击Next按钮进入到第二个activity中去,第二个界面中只有1个Close按钮.当然,据网上有人将要比较安全的实现关闭程序的功能也不是挺简单的,因为android有专门的退出键返回键等.所以该Close按钮暂时没去实现它.我的第1个activity为HelloworldActivity,第2个activity为NextActivity. 实

  • Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)

    在5.2.1节和5.2.2节介绍了<a>标签以及TextView自动识别的特殊文本(网址.电话号.Email等),这些都可以通过单击来触发不同的动作.虽然这些单击动作已经可以满足大多数需要了,但如果读者想在单击链接时执行任意自定义的动作,那么本节的内容非看不可. 现在让我们使用5.2.1节介绍的方法重新查看Html.java文件的内容,随便找一个处理Html标签的方法,例 如,endA方法.该方法用于处理</a>标签.我们会发现在该方法中如下的语句. text.setSpan(ne

随机推荐