在开发过程中,默认的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. 远程访问(post 传参数) 以及IOUtils复制文件

    package com.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream ...

  2. 销售订单增强字段 bapi更新

    如果增强字段在销售订单抬头(vbak)上,则要将增强字段一并append到如下四个表/结构中: VBAKKOZ VBAKKOZX BAPE_VBAK BAPE_VBAKX 在行项目(vbap)上: V ...

  3. 如何查看apache配置文件路径

    我是用https://lamp.sh/安装的,apache配置文件位置在:/usr/local/apache/conf/httpd.conf如果是直接安装的apache,配置文件应该在:/etc/ht ...

  4. Numpy数组的保存与读取方法

    1. 数组以二进制格式保存 np.save和np.load是读写磁盘数组数据的两个主要函数.默认情况下,数组以未压缩的原始二进制格式保存在扩展名为npy的文件中,以数组a为例 np.save(&quo ...

  5. linux中查看python的安装路径

    方法1:whereis python 查看所有python的路径,不止一个 方法2:which python 查看当前使用的python路径

  6. Java基础教程:多线程基础(3)——阻塞队列

    Java基础教程:多线程基础(3)——阻塞队列 快速开始 引入问题 生产者消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据. 模 ...

  7. iOS MVVM+RAC 从基础到demo

    一.关于经典模式MVC的简介 MVC是构建iOS App的标准模式,是苹果推荐的一个用来组织代码的权威范式,市面上大部分App都是这样构建的,具体组建模式不细说,iOS入门者都比较了解(虽然不一定能完 ...

  8. nginx语法之location详解

    Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 ^~ 以某个字符串开头 ~ 区分大小写的正则匹配 ~* 不区分大小写的正则匹配 !~ 区分大小写不匹配的正则 !~* 不区分大小 ...

  9. maven GroupID和ArtifactID

    GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构. ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.一般Gro ...

  10. HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP

    题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others)  ...