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"));
					}
				}


			}
		});
	}


}
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>
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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我将为你提供一个简单的示例代码,演示如何在Android Studio中动态添加项目的TabHost。 1. 首先,在XML布局文件中添加TabHost组件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </TabHost> ``` 2. 在Java代码中找到对应的TabHost组件并初始化: ```java TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 创建一个新的TabSpec对象,用于描述每个标签页: ```java TabHost.TabSpec spec; spec = tabHost.newTabSpec("tag1"); spec.setIndicator("Tab1"); spec.setContent(R.id.tab1); tabHost.addTab(spec); ``` 4. 重复步骤3,以添加其他标签页: ```java spec = tabHost.newTabSpec("tag2"); spec.setIndicator("Tab2"); spec.setContent(R.id.tab2); tabHost.addTab(spec); spec = tabHost.newTabSpec("tag3"); spec.setIndicator("Tab3"); spec.setContent(R.id.tab3); tabHost.addTab(spec); ``` 5. 最后,你可以在代码中动态添加标签页: ```java TabHost.TabSpec spec = tabHost.newTabSpec("tag4"); spec.setIndicator("Tab4"); spec.setContent(new TabHost.TabContentFactory() { @Override public View createTabContent(String tag) { TextView textView = new TextView(MainActivity.this); textView.setText("This is Tab 4"); return textView; } }); tabHost.addTab(spec); ``` 这个示例演示了如何在Android Studio中动态添加项目的TabHost。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值