tabhost动态修改图片和文字

package com.example.tabhost;  
  
import android.app.Activity;  
import android.app.LocalActivityManager;  
import android.content.Intent;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.widget.ImageView;  
import android.widget.TabHost;  
import android.widget.TabHost.OnTabChangeListener;  
import android.widget.TextView;  
  
public class MainActivity extends Activity {  
  
    public static TabHost tabHost;  
    public String[] title ={"融资","帐户","更多"};  
    public View[] tabs = new View[3];  
    private TextView text;  
    private ImageView iv ;  
    public int[] drawableId = { R.drawable.tab1_icon_style,R.drawable.tab2_icon_style, R.drawable.tab3_icon_style };  
    public int[] drawableId1 = { R.drawable.rz,R.drawable.zh1, R.drawable.more11 };  
    public  int[] textcolor = {Color.rgb(255,255,0),Color.rgb(255,0,255),Color.rgb(255,0,255)};  
    private Class context[] = { HomeActivity.class,HomeMyAccountActivity.class, MoreActivity.class };  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        tabHost = (TabHost) findViewById(R.id.tabhost1);  
        LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);  
        mLocalActivityManager.dispatchCreate(savedInstanceState);  
        tabHost.setup(mLocalActivityManager);  
  
  
        for (int i = 0; i < title.length; i++) {  
            tabs[i] = (View) LayoutInflater.from(this).inflate(  
                    R.layout.main_layout, null);  
            text = (TextView) tabs[i].findViewById(R.id.title);  
            iv = (ImageView) tabs[i].findViewById(R.id.icon);  
  
  
            iv.setImageDrawable(getResources().getDrawable(drawableId1[i]));  
            text.setText(title[i]);  
            text.setTextColor(textcolor[i]);  
            Intent intent = new Intent();  
            intent.setClass(this, context[i]);  
            tabHost.addTab(tabHost.newTabSpec(title[i]).setIndicator(tabs[i])  
                    .setContent(intent));  
  
        }  
  
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {  
  
            @Override  
            public void onTabChanged(String tabId) {  
                for (int i = 0; i < title.length; i++) {  
                    if(tabId.equals("融资")){  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(R.id.icon);//获取控件imageView   
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.rz));//改变我们需要的图标  
  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(R.id.icon);  
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.zh1));  
  
  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(2).findViewById(R.id.icon);  
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.more11));  
                        iv.setImageDrawable(getResources().getDrawable(drawableId1[i]));  
  
  
                        text = (TextView)tabHost.getTabWidget().getChildAt(0).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FFFF00"));  
                        text = (TextView)tabHost.getTabWidget().getChildAt(1).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FF00FF"));  
                        text = (TextView)tabHost.getTabWidget().getChildAt(2).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FF00FF"));  
  
                    }  
                    if(tabId.equals("帐户")){  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(R.id.icon);//获取控件imageView   
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.rz1));//改变我们需要的图标  
  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(R.id.icon);  
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.zh));  
  
  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(2).findViewById(R.id.icon);  
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.more11));  
  
  
                        text = (TextView)tabHost.getTabWidget().getChildAt(0).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FF00FF"));  
                        text = (TextView)tabHost.getTabWidget().getChildAt(1).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FFFF00"));  
                        text = (TextView)tabHost.getTabWidget().getChildAt(2).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FF00FF"));  
                    }  
                    if(tabId.equals("更多")){  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(R.id.icon);//获取控件imageView   
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.rz1));//改变我们需要的图标  
  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(R.id.icon);  
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.zh1));  
  
  
                        iv = (ImageView)tabHost.getTabWidget().getChildAt(2).findViewById(R.id.icon);  
                        iv.setImageDrawable(getResources().getDrawable(R.drawable.more));  
  
  
                        text = (TextView)tabHost.getTabWidget().getChildAt(0).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FF00FF"));  
                        text = (TextView)tabHost.getTabWidget().getChildAt(1).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FF00FF"));  
                        text = (TextView)tabHost.getTabWidget().getChildAt(2).findViewById(R.id.title);   
                        text.setTextColor(Color.parseColor("#FFFF00"));  
                    }  
                }  
  
  
            }  
        });  
    }  
  
  
}  
[java] view plain copy
activity_main:<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
  
  
    <TabHost  
        android:id="@+id/tabhost1"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" >  
  
  
        <LinearLayout  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            android:orientation="vertical" >  
  
  
            <FrameLayout  
                android:id="@android:id/tabcontent"  
                android:layout_width="match_parent"  
                android:layout_height="0dp"  
                android:layout_weight="1" >  
            </FrameLayout>  
  
  
            <TabWidget  
                android:id="@android:id/tabs"  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:showDividers="none" >  
            </TabWidget>  
        </LinearLayout>  
    </TabHost>  
  
  
</LinearLayout>  
[java] view plain copy
mian_layout:<!-- tab页中的图标列表 -->  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="wrap_content"  
    android:background="@color/header_background_color"  
    android:layout_height="wrap_content" >  
  
  
    <ImageView  
        android:id="@+id/icon"  
        android:layout_width="20dp"  
        android:layout_height="20dp"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_centerVertical="true"  
        android:layout_marginTop="10dp"  
       />  
  
  
    <TextView  
        android:id="@+id/title"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
        android:layout_centerVertical="true"  
        android:layout_below="@+id/icon"  
        android:paddingBottom="10dp"  
        android:gravity="center"  
        android:textColor="@drawable/tab_txt_color"  
        android:text="TextView" />  
  
  
</RelativeLayout>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值