RelativeLayout 自定义TabHost效果

2024-06-18 02:08

本文主要是介绍RelativeLayout 自定义TabHost效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址 http://www.pocketdigi.com/20110812/442.html


TabHost如果要自定义显示的效果,有点麻烦,而默认的样式有时候又与我们程序的风格不匹配.今天我们就用RelativeLayout来实现与TabHost相同的功能.上效果图:

点击上面的tab,tab自身样式会改变,下面内容也会改变,功能完全与TabHost相同.
介绍一下RelativeLayout,RelativeLayout是相对布局,顾名思义,就是说里面的控件位置都是相对其他控件的位置而确定的.如上面的效果,Tab相对于屏幕顶部对齐,底部按钮相对于屏幕底部对齐.而内容则放在顶部的Tab和底部的按钮中间.
所以所有被其他控件依赖定位的控件,必须先写,也就是说,要实现上面的效果,XML中不是从上往下写,而是先定上和下,再写中间,因为中间的内容高度,位置都依赖于它的上下控件.
实现TabHost效果的原理也简单,点击tab时设置被点击和没被点击的的tab的背景,字体颜色即可显示点击效果.在点击事件中,用View的removeAllViews()方法清除中间控件的所有内容,再用addView方法添加需要的内容.
下面上代码,布局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
50
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:weightSum="1" android:background="@color/white"><LinearLayout android:id="@+id/topmenu"
		android:orientation="horizontal" android:layout_alignParentTop="true"
		android:layout_width="fill_parent" android:layout_height="40dip"
		android:background="@drawable/topback" android:gravity="bottom"><!-- android:layout_alignParentTop 与父元素顶部是否对齐,这里true,就是把这个topmenu放屏幕顶部 --><LinearLayout android:id="@+id/task" android:orientation="horizontal"
			android:layout_height="35dip" android:layout_width="100dip"
			android:background="@drawable/textback"><TextView android:layout_width="fill_parent" android:id="@+id/taskText"
				android:layout_height="fill_parent" android:text="计划" 
				android:gravity="center" android:textSize="20sp" android:textColor="@color/white" /></LinearLayout><LinearLayout android:id="@+id/accounts"
			android:orientation="horizontal" android:layout_height="35dip"
			android:layout_width="100dip"><TextView android:layout_width="fill_parent" android:id="@+id/accountsText"
				android:layout_height="fill_parent" android:text="帐号"
				android:gravity="center" android:textSize="20sp" android:textColor="@color/green"
				 /></LinearLayout><LinearLayout android:id="@+id/sended"
			android:orientation="horizontal" android:layout_height="35dip"
			android:layout_width="100dip"><TextView android:layout_width="fill_parent" android:id="@+id/sendedText"
				android:layout_height="fill_parent" android:text="已发" 
				android:gravity="center" android:textSize="20sp" android:textColor="@color/green" /></LinearLayout></LinearLayout><Button android:id="@+id/button"
		android:layout_alignParentBottom="true" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="底部按钮" /><!-- layout_alignParentBottom 与父元素底部是否对齐,这里true,就是把这个Button放屏幕底部 --><!-- RelativeLayout必须先写四周的控件,再写中间的,这里先写顶部的Tab和底部的Button,再写中间的Content,因为中间的内容位置是不固定的 --><LinearLayout android:id="@+id/content"
		android:orientation="vertical" android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:layout_below="@id/topmenu"
		android:layout_above="@id/button"></LinearLayout><!-- layout_below,当前控件放在设定控件下方 .
			android:layout_above 当前控件放在设定控件上方
			这里配合使用,就是放在顶部tab和底部Button的中间
		--></RelativeLayout>

程序代码:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.pocketdigi;import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;public class Main extends Activity {/** Called when the activity is first created. */LinearLayout task, accounts, sended, content;TextView taskText, accountsText, sendedText;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);task = (LinearLayout) findViewById(R.id.task);accounts = (LinearLayout) findViewById(R.id.accounts);sended = (LinearLayout) findViewById(R.id.sended);content = (LinearLayout) findViewById(R.id.content);taskText = (TextView) findViewById(R.id.taskText);accountsText = (TextView) findViewById(R.id.accountsText);sendedText = (TextView) findViewById(R.id.sendedText);LayoutInflater factory = LayoutInflater.from(this);View taskView = factory.inflate(R.layout.task, null);View accountsView = factory.inflate(R.layout.accounts, null);View sendedView = factory.inflate(R.layout.sended, null);//把三个内容视图的XML文件转化成Viewcontent.addView(taskView);//启动时默认载入taskViewtask.setOnClickListener(new TabListener(task, taskText, taskView));accounts.setOnClickListener(new TabListener(accounts, accountsText,accountsView));sended.setOnClickListener(new TabListener(sended, sendedText,sendedView));//设置三个tab的点击监听器}class TabListener implements OnClickListener {LinearLayout layout;TextView tv;View subView;TabListener(LinearLayout layout, TextView tv, View subView) {this.layout = layout;this.tv = tv;this.subView = subView;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtask.setBackgroundDrawable(null);accounts.setBackgroundDrawable(null);sended.setBackgroundDrawable(null);taskText.setTextColor(getResources().getColor(R.color.green));accountsText.setTextColor(getResources().getColor(R.color.green));sendedText.setTextColor(getResources().getColor(R.color.green));// 全部设为未选中状态layout.setBackgroundResource(R.drawable.textback);tv.setTextColor(getResources().getColor(R.color.white));// 设置选中项content.removeAllViews();//移除所有内容content.addView(subView);//添加传入的View}}
}

Strings.xml中存两个颜色值:

1
2
    <color name="white">#ffffff</color><color name="green">#0cc054</color>

三个内容视图的xml,只贴一个,另两个一样,名字不同而已:
task.xml:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="Task"
  >
</TextView>

这篇关于RelativeLayout 自定义TabHost效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1071004

相关文章

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam