自定义TabWidget
在开发过程中,默认的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的更多相关文章
- android自定义TabWidget样式
先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的 下面是全部代码和流程,一定要按流程顺序来,不然错误! 1.tabhost.xml <TabHost xmlns:android=&q ...
- android自定义TabWidget
在做项目的时候,需要用到这个选项卡,刚开始看了系统的tabwidget,囧了,底边有黑线不说,还不美观,扒了好多的网页发现前辈做的能够满足自己的需求,将代码修改了下,就能用喽,伟人说过,站在前辈的肩膀 ...
- 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)
3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...
- Android 常用UI控件之TabHost(3)在4.0不显示图标的解决方案
1,自定义 TabWidget 上每个tab的view 2,用多个图片
- 自定义TabHost,TabWidget样式
先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图:
- Android 自定义TabHost,TabWidget样式
界面比较简单,要想做得漂亮换几张图片就可以了. 第一步:先在布局(这里用了main.xml创建时自动生成的)里面放上TabHost ,只要将TabHost控件托至屏幕中就可: <?xml ver ...
- Android-修改TabWidget字体大小颜色及对齐
在Android中,我们可以定义TabWidget来分页.在上一篇文章中有说到使用TabWidget定义Tab分页布局,但大部分用户可能会觉得默认的字体有点小,但Tab选项卡默认又不能设定字体大小,如 ...
- Android 实现分页(使用TabWidget/TabHost)
注:本文为转载,但该内容本人已亲身尝试,确认该方法可行,代码有点小的改动,转载用作保存与分享. 原作者地址:http://gundumw100.iteye.com/blog/853967 个人吐嘈:据 ...
- TabHost自定义外观
博客园:http://www.cnblogs.com 农民伯伯: http://www.cnblogs.com/over140 版本 新浪微博 weibo_10235010.apk 正文 一.效果图 ...
随机推荐
- AWS:1.相关概念、创建云主机的过程
概念 EC2是弹性的云计算 云主机 也即虚拟机,由分配的CPU.内存.网络和磁盘等资源组成 好处:维护成本低(主机替换).环境升级成本低 AMI:映像 创建云主机的蓝图,指定初始状态1 预装什么操作系 ...
- Java for LeetCode 131 Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 使用viewholder优化listview,减少findViewById的次数
- windows下的git使用指令
经常使用mac和linux 这次使用window开发了一些小项目 废话不多说: git init git add . git commit -m 'note' git remote add origi ...
- hello vue不显示
本身是做java后端开发的,但对任何技术都感兴趣.于是尝试了下最近国内比较火的vue框架. 在使用官网的例的时候子就卡壳了,写了个html,第一个Hello VUE!就是出不来,只显示{{messag ...
- VLAN虚拟局域网技术(二)-计算机网络
本文主要知识来源于学校课程,部分知识来自于H3C及思科中国公司网页技术手册,未经许可,禁止转载.如需转载,请联系作者并注明出处. 本节主要是总结一些思科的VLAN组中的私有协议:DTP和VTP. 1. ...
- 截取带HTML标签的文本并保留文本样式
一个截取HTML文本的工具,可以按照文字字数或文字字节长度进行截取,保留HTML样式并在最后自动补齐截取后的标签.按工作要求编写,时间紧迫,代码未优化,欢迎讨论和指正.1. [文件] SubHtml ...
- VIM命令总结【转】
命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...
- listen 68 Theoretical Physicist Stephen Hawking Dies at 76
World-renowned British physicist Stephen Hawking, who sought to understand a range of cosmic topics ...
- linux 命令:crontab
一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动c ...