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 ...
随机推荐
- ZigBee协议学习之网络层
ZigBee的体系结构中,底层采用IEEE 802.15.4的物理层和媒介层,再次基础上,ZigBee联盟建立了自己的网络层(NWL)和应用层框架. ZigBee网络层的主要功能包括设备的连接和断开. ...
- DTcms列表隔行换色;loop自带行号
<%loop cdr2 bcategoryList%> <%if(cdr2__loop__id==1)%> <a class="no-bg" href ...
- 【Winform】 Enum逆向解析
将字符串转换成Enum类型 Enum.Parse:将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象. 名称 说明 Parse(Type, String) 将一个或多个枚举常数 ...
- JavaScript笔记(二)——常用数组、字符串方法的应用
1.将字符串中的字符翻转,比如'hello',翻转成'olleh'. var arr=[]; function reverseString(str) { arr=str.split("&qu ...
- ECshop sina
http://blog.sina.com.cn/s/blog_5327408801019ofa.html http://blog.sina.com.cn/u/2624360105 http://blo ...
- WPF中Image控件绑定到自定义类属性
首先我们定义一个Student类,有ID,Name,Photo(保存图片路径). using System; using System.Collections.Generic; using Syste ...
- 2016 Multi-University Training Contest 1 GCD RMQ+二分(预处理)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:有N(N <= 100,000),之后有Q(Q <= 100,000)个区间查询[ ...
- Matlab生成动态链接库供C#调用
1.首先在Matlab中编写一个或几个.m文件 2.然后在命令空间中输入命令:deploytool 3.修改工程名称,修改需要生成文件后缀 4.添加类,添加文件,然后点击生成.
- /****************** Attributes ********************/
/*预定义字符属性的文本.如果钥匙不在字典,然后使用默认值,如下所述. */ 以下属性是IOS6的 NSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS( ...
- Delphi与Qt在Windows下使用共享内存进程间通信
Delphi部分 type TGuardInfo=record Lock: Integer; end; PGuardInfo = ^TGuardInfo; TGuardShareMem=c ...