在开发过程中,默认的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. Netty聊天室-源码

    目录 Netty聊天室 源码工程 写在前面 [百万级流量 聊天室实战]: [分布式 聊天室] [Spring +Netty]: [Netty 原理] 死磕 系列 [提升篇]: [内力大增篇]: 疯狂创 ...

  2. mysql系列之8.mysql高可用 (mha4mysql)

    环境: 三台机器 主服务器: 192.168.1.130 主备机器: 192.168.1.131 监控机器: 192.168.1.132 130和131, 是mysql双主架构 1.在三台机器上安装m ...

  3. POJ - 2251 Dungeon Master 【BFS】

    题目链接 http://poj.org/problem?id=2251 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...

  4. 7-3 堆栈模拟队列(25 point(s)) 【数据结构】

    7-3 堆栈模拟队列(25 point(s)) 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数: int IsFull(Stac ...

  5. node+express上传图片到七牛

    本人微信公众号:前端修炼之路,欢迎关注 最近做项目的时候有一个上传图片的需求,由于没有后端的配合,所以决定自己来搭个服务器,实现上传图片功能.以后如果需要修改成java或者php为后端,直接使用即可, ...

  6. SDUT OJ类型转换函数的应用

    题目描述 处理一个复数与一个double数相加的运算,结果存放在一个double型变量d1中,输出d1的值.定义Complex(复数)类,在成员函数中包含重载类型转换运算符:operator doub ...

  7. POJ2443 Set Operation —— bitset

    题目链接:https://vjudge.net/problem/POJ-2443 Set Operation Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  8. Spring Boot2.0之注解方式启动Springmvc

    回顾下springmvc原理图: DispatcherServlet是Spring MVC的核心,每当应用接受一个HTTP请求,由DispatcherServlet负责将请求分发给应用的其他组件. 在 ...

  9. 迁移博客到Github Pages

    由于种种原因,我的博客迁移到了 https://phuker.github.io/ .虽然没有多少人气,但是希望能继续见证一个技术渣的成长.

  10. 利用jsp技术实现用户注册

    利用jsp技术实现用户注册,包含register.html和register_check.jsp页面代码​1. [代码]J2EE实验    <!DOCTYPE html PUBLIC " ...