自定义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 正文 一.效果图 ...
随机推荐
- plus.os.name 无法正确执行的问题
使用HTML5+开发App的时候, 如果碰到正确的代码却无法出现预期的执行效果, 请检查模块权限配置是否OK? 比如plus.os.name, 需要Device权限 ;
- python 函数中的递归、lambda 、map reduce 等详解
举例说明 #例1: ###递归函数求和 from traitlets.traitlets import Instance def mysum(L): print(L) if not L: return ...
- ABAP excel操作 OLE 常用方法和属性
转自 http://bstone.blog.163.com/blog/static/176820446201172834149199/#userconsent# OLE 常用方法和属性 1.ole中如 ...
- springMvc 4.0 jackson包改变
使用之前的json包出包java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException错误. sp ...
- SQL Server分区表,能否按照多个列作为分区函数的分区依据(转载)
问: Hi, I have a table workcachedetail with 40 million rows which has 8 columns.We decided to partiti ...
- TensorFlow框架(4)之CNN卷积神经网络详解
1. 卷积神经网络 1.1 多层前馈神经网络 多层前馈神经网络是指在多层的神经网络中,每层神经元与下一层神经元完全互连,神经元之间不存在同层连接,也不存在跨层连接的情况,如图 11所示. 图 11 对 ...
- STS、Eclipse报java.lang.OutOfMemoryError: PermGen space 内存溢出
我使用的工具是STS, Eclipse同理: 打开如下界面: 左则选择项目启动使用的Tomcat-->在右侧面板Tab项中选择" Arguments":在VM argumen ...
- ManualResetEvent使用
1.定义 MSDN定义: 通知一个或多个正在等待的线程已发生事件.此类不能被继承. 详细说明: ManualResetEvent 允许线程通过发信号互相通信.通常,此通信涉及一个线程在其他线程进行之前 ...
- Spring Boot2.0之整合log4j
传统方式打印日志比较复杂, 每次打印需要定义全局变量 private static final Logger logger = LoggerFactory.getLogger(SjpControlle ...
- zabbix使用mysql模板监控mysql
出现监控项访问拒绝的信息 解决方法是: 在 mysql的 my.cnf 配置中增加 [mysql] user=zabbix password=zabbix [mysqladmin] user=zabb ...