Android TabHost 动态修改图标或者动态改变标题
那时客户需要实现在TabHost标题上动态显示从数据库获取的个数。起初这样思考的,从数据库 获取个数是非常简单,但是要把获取的个数显示在TabHost标题,思前想后,想用Handler来异步实现消息传递。
如果将图标或者标题的变量设置为全局变量,你们应该知道我的目的吧。也就是为了在这个类不断的进行赋值。
先来简单的认识下TabHost吧。
xml的文件代码:
<?xml version="1.0" encoding="utf-8"?>
<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
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="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
说明一下红色的标注,我们应该不可以改变的吧。
Activity类的代码:
public class MainActivity extends TabActivity
{
//声明TabHost对象
TabHost mTabHost; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //取得TabHost对象
mTabHost = getTabHost(); /* 为TabHost添加标签 */
//新建一个newTabSpec(newTabSpec)
//设置其标签和图标(setIndicator)
//设置内容(setContent)
mTabHost.addTab(mTabHost.newTabSpec("tab_1")
.setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))
.setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_2")
.setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))
.setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_3")
.setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))
.setContent(R.id.textview3)); //设置TabHost的背景颜色
mTabHost.setBackgroundColor(Color.argb(, , , ));
//设置TabHost的背景图片资源
mTabHost.setBackgroundResource(R.drawable.bg0); //设置当前显示哪一个标签
mTabHost.setCurrentTab(); //标签切换事件处理,setOnTabChangedListener
mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
@Override
public void onTabChanged(String tabId)
{
}
});
}
}
转入核心问题吧。
1.将TabHost的标题的图标改变吧。
首先我们应该获取到布局View,再获取对应的控件,图标应该是ImageView,文本应该是TextView。
代码如下:
View mView = mTabHost.getTabWidget().getChildAt();//0是代表第一个Tab
ImageView imageView = (ImageView)mView.findViewById(android.R.id.icon);//获取控件imageView
imageView .setImageDrawable(getResources().getDrawable(R.drawable.img3)); //改变我们需要的图标
imageView = (ImageView)mTabHost.getTabWidget().getChildAt().findViewById(android.R.id.icon);
imageView .setImageDrawable(getResources().getDrawable(R.drawable.img2));
imageView = (ImageView)mTabHost.getTabWidget().getChildAt().findViewById(android.R.id.icon);
imageView .setImageDrawable(getResources().getDrawable(R.drawable.img1));
2.将TabHost的标题改变吧。
TextView textview = (TextView)mTabHost.getTabWidget().getChildAt().findViewById(android.R.id.title);
textview.setTextSize();
textview = (TextView)mTabHost.getTabWidget().getChildAt().findViewById(android.R.id.title);
textview.setTextSize();
textview = (TextView)mTabHost.getTabWidget().getChildAt().findViewById(android.R.id.title);
textview.setTextSize();
其实获取了View,我们还可以再此基础上可以设置标题的颜色啊,字体啊,字体大小等等哦。
Android TabHost 动态修改图标或者动态改变标题的更多相关文章
- Android ActionBar隐藏修改图标和标题
有时候在一些子页面或者内容页面,不需要显示ActionBar的标题栏图标.可用如下方式进行设置. 首先获取到ActionBar对象 ActionBar actionBar=getActionBar() ...
- 微信小程序动态修改title,动态配置title,动态配置头部,微信小程序动态配置头部
微信小程序的title是在json里面配置的 "navigationBarTitleText": "title名称" 这种title是固定死的不灵活处理一些页面 ...
- table动态添加删除一行和改变标题
<style type="text/css"> body{ font-size:13px; line-height:25px; } table{ border-top: ...
- JS动态修改微信浏览器中的title
JS动态修改微信浏览器中的title我们的原理是设置一个ifame然后我们再加载一下就可以实现了,具体的例子如下所示. 平时使用JS修改title,直接document.title=新标题就好了 这样 ...
- Android动态修改ToolBar的Menu菜单
Android动态修改ToolBar的Menu菜单 效果图 实现 实现很简单,就是一个具有3个Action的Menu,在我们滑动到不同状态的时候,把对应的Action隐藏了. 开始上货 Menu Me ...
- android中动态修改ImageView控件的宽高度
本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...
- Ida动态修改android程序的内存数据和寄存器数值,绕过so文件的判断语句
我们继续分析自毁程序密码这个app,我们发现该程序会用fopen ()打开/proc/[pid]/status这个文件,随后会用fgets()和strstr()来获取,于是我们在strstr()处下个 ...
- Android动态修改图片颜色的实现方式分析
版权声明:本文为博主原创文章,未经博主允许不得转载. 1.修改色相.饱和度.亮度 参看:http://blog.csdn.NET/sjf0115/article/details/7267063 2.使 ...
- 动态布局--动态修改RelativeLayout宽高的方法
本文实例讲述了Android编程动态修改RelativeLayout宽高的方法.分享给大家供大家参考,具体如下: 我们经常会动态修改RelativeLayout的宽高,这样的代码,比较简单,就是修改R ...
随机推荐
- Powerful Sleep(神奇的睡眠-睡眠生物钟的秘密:如何睡得更少却睡得更好)阅读笔记
睡眠机制 我们活着的时候,大脑会产生脑电波.脑电图仪器通过贴在人头上的一些电极读出脑电波的活动,然后把活动用图表显示出来. 睡眠过程可以分为5个过程,划分依据与大脑发出的脑电波类型. 当人清醒时,大脑 ...
- Sublime Text 破解
引言 放假三天,呆家里把win7换成了win8.1,接着玩起了hyperv,试着装了个windows xp虚拟机,体验很不错.不过对linux系统的支持不怎么样,装了个ubuntu,体验相当差!闲着无 ...
- 随机森林之oob error 估计
摘要:在随机森林之Bagging法中可以发现Bootstrap每次约有1/3的样本不会出现在Bootstrap所采集的样本集合中,当然也就没有参加决策树的建立,那是不是意味着就没有用了呢,答案是否定的 ...
- Less和Sass编译
使用koala编译 Koala 是一款由国人开发的开源预处理语言图形编译工具,目前已支持 Less.Sass.Compass 与CoffeeScript. 目前支持以下系统:Windows,Mac, ...
- ListView练习
1. 在 .xml中创建一个ListView是不会显示出来的. 2. ListView的Item: 列表项, 3. 显示ListView的4个要素: 3.1 ListView控件:在layout布局中 ...
- NOSQL之【redis的主从复制】
一.Redis的Replication: 下面的列表清楚的解释了Redis Replication的特点和优势. 1). 同一个Master可以同步多个Slaves. 2). Slave同 ...
- ubuntu find方法
通用格式:find pathname -options [-print -exec -ok]例子:find / -name filename 再根目录里面搜索文件名为filename的文件find / ...
- 【趟坑】公共引用的jar包 pom的配置方法
http://www.cnblogs.com/viewcozy/p/4789877.html 接上一篇 ,内部moudule生成jar的方式 上一篇已经实现了,想把jar作为公共的部分让任何项目都可以 ...
- 可视化Windows服务定时任务
思路:1.新建一个<Windows服务项目>,在里面建服务文件(可以多个),用来做要执行的任务. 2.新建一个<Windows窗体应用程序>,在这个程序里管理服务.如:安装.卸 ...
- EXTJS 4.2 资料 控件之Grid 列鼠标悬停提示
columns: [ { header: }, { header: }, { header: , renderer: function (v, ctx, record) { ctx.tdAttr = ...