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

相关文章

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依