- 浏览: 799530 次
- 性别:
- 来自: 哈尔滨
文章分类
最新评论
-
doloveme:
$sudo cp MONACO_Linux.ttf /usr/ ...
terminator 的配置 -
腾讯rep:
lowser
RSA加密算法c++简单实现 -
csuhanyong:
好YD啊,好YD
一图流:机械键盘各轴区别 -
doloveme:
1。其中:extman ,extmail下载http://ww ...
Ubuntu下安装postfix+extmail邮件服务器 -
doloveme:
repo sync出现“fatal: '../platform ...
Android 编译环境配置搭建(Ubuntu 12.04)及环境导致编译错误QA
TabHost生成后会向内添加TabSpec以形成选项栏:
如果是继承的TabActivity类:
- tabhost = getTabHost();
- tabhost.addTab(tabhost.newTabSpec("1" ).setIndicator( "TAB 1" , getResources().getDrawable(R.drawable.icon)).setContent(R.id.text1));
- tabhost.setCurrentTab(0 );
如果是直接继承Activity类:
- tabhost = (TabHost) findViewById(R.id.tabhost);
- tabhost.setup();
- tabhost.addTab(tabhost.newTabSpec("1" ).setIndicator( "TAB 1" , getResources().getDrawable(R.drawable.icon)).setContent(R.id.text1));
- tabhost.setCurrentTab(0 );
注意加.setup(),否则会有NullPointer的异常
main.xml:
- < TabHost android:id = "@+id/tabhost" android:layout_width = "fill_parent" android:layout_height = "wrap_content" >
- < LinearLayout android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" >
- < TabWidget android:id = "@android:id/tabs" android:layout_width = "fill_parent" android:layout_height = "fill_parent" />
- < FrameLayout android:id = "@android:id/tabcontent" android:layout_width = "fill_parent" android:layout_height = "fill_parent" >
- < TextView android:id = "@+id/text1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" />
- </ FrameLayout >
- </ LinearLayout >
- </ TabHost >
注意:TabHost标签下的TabWidget和FrameLayout的id 是固定的,分别为"tabs"和"tabcontent"
---------------------------------------------------------------------------------- 分割线 ---------------------------------------------------------------------------------------------
动态更改TabHost的选项卡头显示,有两种方法:一种比较笨而且可控性差(为 什么还要讲一会再说),一种比较简单而且可控性高,我们先说简单的那一种:
TabHost本身不提供有效的函数支持对选项卡头的动态更改,但是我们可以通过 TabHost的getTabWidget()函数得到选项卡头的View从而"曲线"更改选项卡头的属性:
- ImageView iv = (ImageView) tabhost.getTabWidget().getChildAt(targetLocation).findViewById(android.R.id.icon);
- TextView tv = (TextView) tabhost.getTabWidget().getChildAt(targetLocation).findViewById(android.R.id.title);
- tv.setText("XXXX" );
- iv.setImageDrawable(getResources().getDrawable(R.drawable.icon));
其中获得的ImageView即是TabHost的选项卡
头在targetLocation位置的图片的View,TextView是对应的文字的View,这样就可以对它们进行任意的改动了,注意
targetLocation从0开始
还有一种比较笨的方法:
就是自定义TabHost选项卡头的Layout,如下:
- tabhost = getTabHost();
- RelativeLayout tempLayout = (RelativeLayout) LayoutInflater.from(this ).inflate(R.layout.mytab, null );
- TextView targetTabLabel = (TextView) tempLayout.findViewById(R.id.tab_text);
- targetTabLabel.setText("TAB 1" );
- tempLayout.removeAllViews();
- tabhost.addTab(tabhost.newTabSpec("1" ).setIndicator(targetTabLabel).setContent(R.id.text1));
注意要对tempLayout使用removeAllViews(),否则在将 targetTabLabel加入TabHost中时会出现java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.的异常
其中R.layout.mytab来自mytab.xml:
- <? xml version = "1.0" encoding = "utf-8" ?>
- < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content" >
- < TextView android:id = "@+id/tab_text"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:gravity = "center"
- android:textStyle = "bold"
- android:background = "@drawable/selector_img" />
- </ RelativeLayout >
其中android:text="@drawable/selector_img"指向
selector_img.xml:
- <? xml version = "1.0" encoding = "utf-8" ?>
- < selector xmlns:android = "http://schemas.android.com/apk/res/android" >
- < item android:state_selected = "false"
- android:drawable = "@drawable/img1" >
- </ item >
- < item android:state_selected = "true"
- android:drawable = "@drawable/img2" >
- </ item >
- </ selector >
点击时使用Selector置换背景图片,文字的更改尚未用此方法验证,这种方法非常繁琐而且可控性太差,只能响应选择事件,且只能更改2个选项,但是之
所以讲解这种方法,是因为这里顺便讲解了如何自定义TabHost选项卡头的Layout,如果你想在UI上做出更多的花样,这个技巧是很有帮助的。
发表评论
-
JAVA代码混淆
2016-05-05 18:24 1364JAVA代码混淆 <!--[if !supportL ... -
android 监听状态栏被下拉
2015-12-25 17:23 3372Activity的onWindowFocusChange()方 ... -
android使用mount挂载/system/app为读写权限,删除或替换系统应用
2015-11-27 14:13 3946注意:以下代码中#开头的则为需要执行的shell命令,其他的 ... -
Ubuntu 14.04 文件服务器--samba的安装和配置
2015-11-10 10:49 598jingyan.baidu.com/album/00a07f ... -
intellij 打开工程在每个java文件上 有个红色的无效符
2015-07-24 13:52 1707说明该类不是可编译文件。在project Structure中 ... -
logcat命令详解
2015-07-22 16:34 926Android日志系统提供了记录和查看系统调试信息的功能。日 ... -
gradle打包遇到的错误汇总
2015-07-21 11:32 0错误1:A problem occurred ... -
Android读写XML(下)——创建XML文档
2015-04-29 11:02 569在前面的2篇文章Android读写XML(上)—— pack ... -
Android读写XML(中)——SAX
2015-04-29 10:54 588在Android读写XML(上) ... -
Android读写XML(下)——创建XML文档
2015-04-29 10:52 659在前面的2篇文章Android读写XML(上)—— pack ... -
android:configChanges locale 改语言后,该配置不起作用的原因
2015-03-23 14:38 2340@Override public void on ... -
android.app.Fragment$InstantiationException 解决办法
2015-03-23 13:46 1309android.app.Fragment$Instantia ... -
linux 查看内存
2015-02-10 13:08 6931、free监控内存使用情况 #free 2、watc ... -
android fastboot 刷机 指令
2015-01-19 18:31 1464原生bootloader:bootloader-gro ... -
[Android实例] android中进行https连接的方式的详解 (转发)
2015-01-09 15:50 665x 如果不需要验证服务器端证书, ... -
编译android源码apk是否生成odex方法
2014-10-20 17:19 1577其实Android系统默认的配置是,如果没有指定DISABL ... -
关于android源码中的APP编译时引用隐藏的API出现的问题
2014-10-20 15:36 769今天在编译android源码中的计算器APP时发现,竟然无法 ... -
Android系统进程Zygote启动过程的源代码分析
2014-09-17 10:09 452在Android系统中, ... -
Handler和HandlerThread
2014-09-16 16:58 6531.什么是Handler? SDK中关于Handler的说 ... -
怎样识别三洋18650锂电池电芯
2014-09-01 09:52 1063浏览:20384 | 更新:2011-03 ...
相关推荐
本文实例分析了Android开发之TabHost选项卡及相关疑难解决方法。分享给大家供大家参考,具体如下: 前言: 虽然现在谷歌已经不推荐使用TabHost,但是初学者还是很有必要接触下这一成金的经典的,本文将介绍纤细介绍...
TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计。 一、基础知识 TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡; TabSpec : 代表了选项卡界面, 添加...
Android实现手机界面中的TAB,也就是大家熟悉的选项卡效果,可以单击或轻触选项卡,切换主体部分的内容,目前在各种手机应用软件中已得到普遍使用,这是一个创建基本TAB选项卡的例子,使用了TabHost实现,运行效果如...
首先,定义TabHost的布局文件: 代码如下:<?xml version=”1.0″ encoding=”utf-8″?><TabHost xmlns:android=”http://schemas.android.com/apk/res/android” android:id android:layout_width=”fill_...
<TabHost xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@android:id/tabhost” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <LinearLayout ...
以下通过TabHost实现android选项卡。 main.xml布局文件: <?xml version=1.0 encoding=utf-8?> android:layout_width=fill_parent android:layout_height=fill_parent> <LinearLayout android:id=@+i
TabHost布局文件 我们先来了解一下布局文件的基本内容: 1. 根标签及id 设置Android自带id : XML布局文件中, 可以使用 标签设置...tabhost android:id=”@android:id/tabhost” android:layout_height=”match_parent”
选项卡(TabHost)方便的在窗口上设置多个标签页,每个标签页相当于获得一个与外部容器相同大小的组件摆放区域 通过这种方式,可以在一个容器中放置多组件。 创建4个java文件并对应layout 创建主java ,代码 ...
现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能。 我们先看下该demo实例的框架图: 其中各个类的作用以及...
本文实例讲述了Android编程实现在底端显示选项卡的方法。分享给大家供大家参考,具体如下: 1.layout 文件 <?xml version=1.0 encoding=utf-8 ?> android:orientation android:layout_width=fill_parent ...
/*添加第一个选项卡*/ TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1"); tab1.setIndicator("login"); tab1.setContent(new Intent(this,LoginActivity.class)); tabHost.addTab(tab1); /*添加第二个选项卡*/ ...
本文实例讲述了Android编程实现将tab选项卡放在屏幕底部的方法。分享给大家供大家参考,具体如下: 今天写Tab的时候由于TAB的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将TAB放置在屏幕的底端。有点类似...
2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView)的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 ...
看到很多热门的Android程序(如:新浪微博、腾讯微博、京东商城、淘宝、当当等等)使用选项卡风格作为程序界面的主框架结构,而Android的选项卡控件默认是按钮在上方的。我在网上看到有多种实现方法,这里提供一种...
2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView) 的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图...
2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView) 的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图...