动态切换TabHost图标

动态切换TabHost图标,今天调整布局的时候需要调整跟新浪微薄tabhost切换的那种效果

先看图


这里只copy项目中的主要代码

private int[] resArray = new int[5];//存放默认的5张图标
private int[] selectResArray = new int[5];//存放选中的5张图标

//把资料文件中的图片分别存放在这两个数组中去(取资源中的图片我是通过配置文件取的,具体怎么取,Google一下吧)

                    int resID = getResources().getIdentifier(mainmenus.get(i).getKey(), "drawable",appInfo.packageName);// 找到底部菜单图片资源文件ID
                    //选中高亮图标
                    int selectResId =  getResources().getIdentifier(mainmenus.get(i).getKey()+"_sel", "drawable",appInfo.packageName);
                    if (resID == 0) {
                        resID = getResources().getIdentifier(
                                "menu_"+ activity.toLowerCase().substring(0, 1)+ activity.substring(1), "drawable",
                                appInfo.packageName);
                        selectResId = getResources().getIdentifier(
                                "menu_"+ activity.toLowerCase().substring(0, 1)+ activity.substring(1)+"_sel", "drawable",
                                appInfo.packageName);
                    }
                    resArray[i] = resID;
                    selectResArray[i] = selectResId;


//下面就是把数据里面的值循环放进去
tabhost.setOnTabChangedListener(new OnTabChangeListener() {
			public void onTabChanged(String tabId) {
				for (int i = 0; i < tabWidget.getChildCount(); i++) {
					View v = tabWidget.getChildAt(i);
					ImageView imageView = (ImageView)v.findViewById(R.id.tab_img);
					imageView.setImageResource(resArray[i]);
				}
				View v = tabWidget.getChildAt(Integer.parseInt(tabId));
				ImageView imageView = (ImageView)v.findViewById(R.id.tab_img);
				imageView.setImageResource(selectResArray[Integer.parseInt(tabId)]);
			}
		});

代码就这么多,,大家看看看吧

  • 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、付费专栏及课程。

余额充值