自定义TabWidget
2024-10-20 06:23:03
在开发过程中,默认的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); } } |
实现的效果:
最新文章
- JavaScript 开发规范
- 【生活没有希望】hdu1166敌兵布阵 线段树
- 分布式架构中一致性解决方案——Zookeeper集群搭建
- 关于H5本部缓存localStorage,sessionStorage
- HDU 5884 Sort -2016 ICPC 青岛赛区网络赛
- home page
- gridView使用
- 最长上升子序列(LIS)模板
- YTU 2019: 鞍点计算
- nasm fasm yasm 还是masm、gas
- 李洪强漫谈iOS开发[C语言-008]- C语言重难点
- Leetcode 242. Valid Anagram(有效的变位词)
- Python之路【第十三篇】:jQuery -暂无内容-待更新
- WebService的基本介绍
- [蓝桥杯]PREV-22.历届试题_国王的烦恼
- windown 下最简单的安装mysql方式
- Linux下的好用的编辑软件Remarkable
- Swift 构造与析构
- nodejs(三)Buffer module &; Byte Order
- Oracle新建数据库,并导入dmp文件
热门文章
- 【题解】数字组合(NTT+组合 滑稽)
- JavaScript演示如何访问Search字段
- 关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法
- ME51N, ME52N 创建采购申请的一个BADI
- 负载均衡,会话保持,session同步(转)
- before-request , after-request
- Java多线程系列 基础篇05 synchronized关键字
- bzoj5213: [Zjoi2018]迷宫
- Spring Boot2.0之 原理—创建内置Tomcat容器
- 应用程序无法启动(0*c000007b)