在开发过程中,默认的TabWidget不能满足我们对于UI的要求并且没有足够的属性工我们去修改,这个时候能够自定义TabWidget是非常必要的。自定义TabWidget组要运用的是TabSpec.setIndicator(View view)方法。

main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main" >
 
    <RelativeLayout
        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_alignParentBottom="true" />
 
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
 
            <LinearLayout
                android:id="@+id/tabs1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="我是tab1" />
            </LinearLayout>
 
            <LinearLayout
                android:id="@+id/tabs2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
 
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="我是tab2" />
            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>
 
</TabHost>

供点击时切换的图片tabmini.xml:

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:drawable="@drawable/check" android:state_selected="true"/>
    <item android:drawable="@drawable/uncheck" android:state_selected="false"/>
 
</selector>

自定义view的布局文件custom.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/tabmini" />
 
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0sp"
        android:gravity="center_horizontal"/>
 
</LinearLayout>

最后是我们的main.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.app.main;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
 
public class Main extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        TabHost tabHost = (TabHost) this.findViewById(android.R.id.tabhost);
 
        // tabhost如果是以findViewById()这个方法获取的,必须调用setup()方法
        tabHost.setup();
 
        View view1 = this.getLayoutInflater().inflate(R.layout.custom, null);
 
        TextView tv1 = (TextView) view1.findViewById(R.id.tv);
 
        tv1.setText("tab1");
 
        View view2 = this.getLayoutInflater().inflate(R.layout.custom, null);
 
        TextView tv2 = (TextView) view2.findViewById(R.id.tv);
 
        tv2.setText("tab2");
 
        TabSpec spec1 = tabHost.newTabSpec("tab1").setIndicator(view1)
                .setContent(R.id.tabs1);
 
        TabSpec spec2 = tabHost.newTabSpec("tab2").setIndicator(view2)
                .setContent(R.id.tabs2);
 
        tabHost.addTab(spec1);
 
        tabHost.addTab(spec2);
 
    }
 
}

实现的效果:

自定义TabWidget的更多相关文章

  1. android自定义TabWidget样式

    先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的 下面是全部代码和流程,一定要按流程顺序来,不然错误! 1.tabhost.xml <TabHost xmlns:android=&q ...

  2. android自定义TabWidget

    在做项目的时候,需要用到这个选项卡,刚开始看了系统的tabwidget,囧了,底边有黑线不说,还不美观,扒了好多的网页发现前辈做的能够满足自己的需求,将代码修改了下,就能用喽,伟人说过,站在前辈的肩膀 ...

  3. 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)

    3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...

  4. Android 常用UI控件之TabHost(3)在4.0不显示图标的解决方案

    1,自定义 TabWidget 上每个tab的view 2,用多个图片

  5. 自定义TabHost,TabWidget样式

    先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图:

  6. Android 自定义TabHost,TabWidget样式

    界面比较简单,要想做得漂亮换几张图片就可以了. 第一步:先在布局(这里用了main.xml创建时自动生成的)里面放上TabHost ,只要将TabHost控件托至屏幕中就可: <?xml ver ...

  7. Android-修改TabWidget字体大小颜色及对齐

    在Android中,我们可以定义TabWidget来分页.在上一篇文章中有说到使用TabWidget定义Tab分页布局,但大部分用户可能会觉得默认的字体有点小,但Tab选项卡默认又不能设定字体大小,如 ...

  8. Android 实现分页(使用TabWidget/TabHost)

    注:本文为转载,但该内容本人已亲身尝试,确认该方法可行,代码有点小的改动,转载用作保存与分享. 原作者地址:http://gundumw100.iteye.com/blog/853967 个人吐嘈:据 ...

  9. TabHost自定义外观

    博客园:http://www.cnblogs.com 农民伯伯: http://www.cnblogs.com/over140 版本 新浪微博 weibo_10235010.apk 正文 一.效果图 ...

随机推荐

  1. SQL中的四种连接方式

    转自:http://www.cnblogs.com/afirefly/archive/2010/10/08/1845906.html 联接条件可在FROM或WHERE子句中指定,建议在FROM子句中指 ...

  2. git查看某一次commit里面的内容,即本次commit相对于原来的版本进行了哪些修改

    1 知道commit id的话 git show commit-id 2 想要查看某次commit的某个文件进行了哪些修改 git show commit-id filename

  3. Java反射机制简单学习

    java中除了基本数据类型,几乎都为对象.例如 Person p=new Person(); 这句语句表明了p是Person类的一个实例对象.但其实,Person也是一个实例对象,它是Class类的实 ...

  4. Python实现简易HTTP服务器

    一.Python3 搭建简易HTTP服务器 python -m http.server 浏览器访问:http://localhost:8000 Python3 cgiserver python -m  ...

  5. 使用qt+ros调用摄像头遇到的问题

    当使用摄像头遇到如下问题:[usb_cam-1] process has died [pid 12288, exit code 127, cmd /opt/ros/indigo/lib/usb_cam ...

  6. SQL 中GROUP BY 、ROLLUP、CUBE 关系和区别

    转自:http://www.cnblogs.com/dyufei/archive/2009/11/12/2573974.html 不言自明,看SQL就完全理解了,不需要过多解释,不错,分享之: ROL ...

  7. API的理解和使用——字符串的命令

    字符串的命令复习表 命令 作用 set   setex   setnx   get   mset   mget   incr   decs   incrby   decrby   incrbyfloa ...

  8. Java for LeetCode 102 Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. 版本名称SNAPSHOT、alpha、beta、release、GA含义

    Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用.Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出.RC:(Release Candida ...

  10. win10安装tomcat7

    下载Tomcat 安装tomcat tomcat7是绿色软件,解压后即可使用,请大家先将tomcat解压到合适的位置(建议整个路径都是英文路径),下载 apache-tomcat-7.0.79-win ...