那时客户需要实现在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 动态修改图标或者动态改变标题的更多相关文章

  1. Android ActionBar隐藏修改图标和标题

    有时候在一些子页面或者内容页面,不需要显示ActionBar的标题栏图标.可用如下方式进行设置. 首先获取到ActionBar对象 ActionBar actionBar=getActionBar() ...

  2. 微信小程序动态修改title,动态配置title,动态配置头部,微信小程序动态配置头部

    微信小程序的title是在json里面配置的 "navigationBarTitleText": "title名称" 这种title是固定死的不灵活处理一些页面 ...

  3. table动态添加删除一行和改变标题

    <style type="text/css"> body{ font-size:13px; line-height:25px; } table{ border-top: ...

  4. JS动态修改微信浏览器中的title

    JS动态修改微信浏览器中的title我们的原理是设置一个ifame然后我们再加载一下就可以实现了,具体的例子如下所示. 平时使用JS修改title,直接document.title=新标题就好了 这样 ...

  5. Android动态修改ToolBar的Menu菜单

    Android动态修改ToolBar的Menu菜单 效果图 实现 实现很简单,就是一个具有3个Action的Menu,在我们滑动到不同状态的时候,把对应的Action隐藏了. 开始上货 Menu Me ...

  6. android中动态修改ImageView控件的宽高度

    本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...

  7. Ida动态修改android程序的内存数据和寄存器数值,绕过so文件的判断语句

    我们继续分析自毁程序密码这个app,我们发现该程序会用fopen ()打开/proc/[pid]/status这个文件,随后会用fgets()和strstr()来获取,于是我们在strstr()处下个 ...

  8. Android动态修改图片颜色的实现方式分析

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.修改色相.饱和度.亮度 参看:http://blog.csdn.NET/sjf0115/article/details/7267063 2.使 ...

  9. 动态布局--动态修改RelativeLayout宽高的方法

    本文实例讲述了Android编程动态修改RelativeLayout宽高的方法.分享给大家供大家参考,具体如下: 我们经常会动态修改RelativeLayout的宽高,这样的代码,比较简单,就是修改R ...

随机推荐

  1. C#中调用unmanaged DLL

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. C++记录2

    1, 求成员变量的偏移: 2, const实现机制:在编译期间完成,对于内置类型,如int, 编译器可能使用常数直接替换掉对此变量的引用.而对于结构体不一定. 编译器在优化代码时把j直接优化成64h了 ...

  3. 基于IOS和Android设备MDM技术方案服务价格

    导读:前段时间 www.mbaike.net 博客被恶意攻击,导致程序崩溃,目前已经替换了以前的Wordpress程序,现提供IOS和Android版本MDM的代码和相关文档咨询服务. 一.IOS版M ...

  4. 如何Angularjs1.3在页面中输出带Html标记的文本

    基于安全考虑,Angularjs不允许用ng-bind或者{{}}的方法输出html文本. 在实际的应用中,比如信息管理系统,用在线编辑器编辑出来的文章都带有html标记,这种情况下可以用ng-bin ...

  5. android studio引入第三方包记录

    1.添加jar文件 将jar文件复制至app module目录下的libs文件夹下,然后打开app module目录下的build.gradle配置文件,在dependencies项中添加配置命令,这 ...

  6. C#快速学习笔记(译)

    下面是通过代码快速学习C#的例子. 1.学习任何语言都必定会学到的hello,world! using System; public class HelloWorld { public static ...

  7. [python] 字符串引用

    %s str %d 整数 %f 浮点数 print('$%.03f' % 30.1777) >>>$30.178  #四舍五入 print( '%-5s %s %10s' % ('J ...

  8. UVA 11059

    Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the ...

  9. 【dapper】.net平台下的框架

    http://www.cnblogs.com/yipu/archive/2012/11/21/2780199.html Method Duration Remarks Hand coded (usin ...

  10. 分类-Category

    1.基本用途 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 继承 分类(Category) 2.格式 分类的声明 @interface 类名 (分类名称) // 方法声明 @end 分 ...