- 浏览: 325217 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
xuanyuanxiaoxue:
...
Android - LayoutInflater -
柴兴博:
不错 多谢
Android 悬浮Activity并可拖动(访悬浮歌词) -
di1984HIT:
写的很好,我收藏一下。
java之动态代理模式(JDK和cglib) -
chinacssnj:
待测试,明天测,测试的结果发给大家
网络开发上传文件到服务器 -
fx_199182:
...
Android之MediaPlayer
android之Tabhost深入讲解二
- 博客分类:
- Android之控件及布局
之前一直认为TabHost不够灵活而使用ActivityGroup。看到了一个利用反射来控制TabHost的例子
先通过
tabHost.newTabSpec(CagConstant.TAB_TAG_CHANGE_INFO); tabHost.newTabSpec(CagConstant.TAB_TAG_ROUTE_INFO); tabHost.newTabSpec(CagConstant.TAB_TAG_STATION_INFO);
来获得TabSpec单个按钮的对象,之后setContent(ViewId)来设置选中状态效果。接下来通过反射
tabWidget.getClass().getDeclaredField("mBottomLeftStrip"); tabWidget.getClass().getDeclaredField("mBottomRightStrip"); (2.2之后改为mLeftStrip,mRightStrip)
得到TabSpec左右两侧按钮的Field对象。然后解除访问限制setAccessible(true);设置未被选中时的背景。点击事件按钮背景的变化是通过for循环来判断的,个人感觉还有可以改进的地方。
package com.CustomTabhost; import java.lang.reflect.Field; import com.tab.R; import android.app.TabActivity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabWidget; import android.widget.TextView; public class CustomTabhost extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int width = 45; int height = 30; final TabHost tabs = getTabHost(); final TabWidget tabWidget = tabs.getTabWidget(); //-----------------------------若是继承自Activity OR ActivityGroup----------------------- // TabHost tabHost=(TabHost)findViewById(R.id.tabhost); // tabs.addTab(tabs.newTabSpec("first tab").setIndicator("信息").setContent(new Intent(CustomTabhost.this, Load.class))); // 注意此处我们用的是setContent(new Intent(CustomTabhost.this, Load.class)) // 那么tabHost.setup(this.getLocalActivityManager());必须继承ActivityGroup // 若是用的是类似:mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.LinearLayout01)); // 则应该用tabHost.setup(); 可以继承自Activity OR ActivityGroup // TabWidget tabWidget = tabs.getTabWidget();//此句话应该在setup方法后面,不然会为null,若是继承自TabActivity则不用考虑因为TabActivity已经帮我们封装实现了 //--------------------------------------------------------------------------------------- Field mBottomLeftStrip; Field mBottomRightStrip; // LayoutInflater.from(this).inflate(R. layout.loading, // tabs.getTabContentView(), true); tabs.addTab(tabs.newTabSpec("first tab").setIndicator("信息").setContent( new Intent(CustomTabhost.this, Load.class))); tabs.addTab(tabs.newTabSpec("second tab").setIndicator("收藏") .setContent(new Intent(CustomTabhost.this, Load.class))); tabs.addTab(tabs.newTabSpec("second tab").setIndicator("设置") .setContent(new Intent(CustomTabhost.this, Load.class))); tabs.setCurrentTab(1); for (int i = 0; i < tabWidget.getChildCount(); i++) { /** * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果 */ tabWidget.getChildAt(i).getLayoutParams().height = height; tabWidget.getChildAt(i).getLayoutParams().width = width; /** * 设置tab中标题文字的颜色,不然默认为黑色 */ final TextView tv = (TextView) tabWidget.getChildAt(i) .findViewById(android.R.id.title); tv.setTextColor(this.getResources().getColorStateList( android.R.color.white)); /** * 此方法是为了去掉系统默认的色白的底角 * * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip * 都是私有变量,但是我们可以通过反射来获取 * * 由于还不知道Android 2.2的接口是怎么样的,现在先加个判断好一些 */ if (Integer.valueOf(Build.VERSION.SDK) <= 7) { try { mBottomLeftStrip = tabWidget.getClass().getDeclaredField( "mBottomLeftStrip"); mBottomRightStrip = tabWidget.getClass().getDeclaredField( "mBottomRightStrip"); if (!mBottomLeftStrip.isAccessible()) { mBottomLeftStrip.setAccessible(true); } if (!mBottomRightStrip.isAccessible()) { mBottomRightStrip.setAccessible(true); } mBottomLeftStrip.set(tabWidget, getResources().getDrawable( R.drawable.no)); mBottomRightStrip.set(tabWidget, getResources() .getDrawable(R.drawable.no)); } catch (Exception e) { e.printStackTrace(); } } else { try { mBottomLeftStrip = tabWidget.getClass().getDeclaredField( "mLeftStrip"); mBottomRightStrip = tabWidget.getClass().getDeclaredField( "mRightStrip"); if (!mBottomLeftStrip.isAccessible()) { mBottomLeftStrip.setAccessible(true); } if (!mBottomRightStrip.isAccessible()) { mBottomRightStrip.setAccessible(true); } mBottomLeftStrip.set(tabWidget, getResources().getDrawable( R.drawable.no)); mBottomRightStrip.set(tabWidget, getResources() .getDrawable(R.drawable.no)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } View vvv = tabWidget.getChildAt(i); if (tabs.getCurrentTab() == i) { vvv.setBackgroundDrawable(getResources().getDrawable( R.drawable.navbgfocus)); } else { vvv.setBackgroundDrawable(getResources().getDrawable( R.drawable.menubg)); } } /** * 当点击tab选项卡的时候,更改当前的背景 */ tabs.setOnTabChangedListener(new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub for (int i = 0; i < tabWidget.getChildCount(); i++) { View vvv = tabWidget.getChildAt(i); if (tabs.getCurrentTab() == i) { vvv.setBackgroundDrawable(getResources().getDrawable( R.drawable.menufocus)); } else { vvv.setBackgroundDrawable(getResources().getDrawable( R.drawable.menubg)); } } } }); } }
发表评论
-
Android 漂亮的 Dialog (弹出框)
2012-02-13 23:35 4196在新浪微博Android客户端的开发的课程中呢要用的得自定义的 ... -
ListView的分页显示
2012-01-13 11:22 1205package com.anddev.ListMore.Tes ... -
ProgressBar的圆形和长条形设置
2012-01-12 15:02 0默认的progresbar是圆形的旋转动画: <Pro ... -
把TextView中的文字添加阴影效果及Style的使用
2012-01-12 15:00 2064<TextView android:id=" ... -
GridView动态加载数据情况下,选中状态的实现
2012-01-12 14:57 1453GridView使用AdapterView动态加载数据情况下, ... -
Android中的透明属性的设置
2012-01-12 14:50 1246ui设计时,需要指定某个元素的drawable时,有时候需要告 ... -
简单实现ImageView的加边框颜色的方法
2012-01-12 14:48 2326对于一个ImageView,如果我们要加上边框,比如宽为3dp ... -
android ,点击title的ImageView弹出PopupWindow【安卓进化七】
2011-12-03 10:33 1399一、曾经做过一个项目,用的大概就是这种布局,title布局,t ... -
Android之PopupWindow弹出对话框
2011-12-03 09:40 4075Android 的 对话框常 ... -
CursorTreeAdapter 详解
2011-12-02 17:44 2180CursorTreeAdapter 通过该适配类可以用一连续的 ... -
Android 应用程序窗体显示状态操作(requestWindowFeature()的应用)
2011-12-02 09:43 754我们在开发程序是经常会需要软件全屏显示、自定义标题(使用按钮等 ... -
Android GestureDetector 手势基础
2011-12-01 16:34 12751. 当用户触摸屏幕的时候,会产生许多手势,例如dow ... -
Android GestureDetector手势识别类
2011-12-01 14:26 2759为了加强鼠标响应事件,Android提供了GestureDet ... -
android之Tabhost深入讲解一
2011-11-28 21:46 1677大家都知道Tabhost中Tabwidget的布局是图标在上, ... -
Android中的view
2011-11-21 15:02 1144View类是Android的一个超类,这个类几乎包含了所有的屏 ... -
Android自定义View之一:初探实例 .
2011-11-21 14:53 796Android自定义View实现很简单 继承View,重写构 ...
相关推荐
Android 嵌套TabHost示例. 两层TabHost嵌套,实现的效果类似网页上的二级菜单栏。
Android tabhost 详细讲解
这份Pdf文档主要讲解了TabHost控件的使用方法,希望对菜鸟们有帮助
Android实现tabHost控件自定义页签页面源码
Android_TabHost_TabWidget选项卡总结
android tabhost activitygroup 源码 分享给有需要的朋友们
Android studio开发,布局TabHost布局 demo,包括修改tab布局: tab栏字体颜色,tab栏大小等布局.
Android的tabHost案例,适合初学者的Android的tabHost案例,适合初学者,里面有注释,代码清晰明了,导入项目即可使用,亲测可用
Android Tabhost 实例demo 自定义Tabhost实现实例。
android 为TabHost添加标签的实现源码,新建一个newTabSpec(newTabSpec),设置其标签和图标(setIndicator),设置内容(setContent),设置TabHost的背景颜色,设置TabHost的背景图片资源,mTabHost....
androidTabhost的使用实例代码,仅供参考
android Tabhost使用Demo
Android TabHost动态增加和删除
Android使用tabhost源码
android 最新的TabHost,不是传统的TabHost,继承了FrameActivity.
android通过tabhost实现标签页
Android TabHost ListView初学者的实例代码
自定义Android中TabHost组件显示在屏幕底部,并实现滑动切换页面。在此基础上可以实现类似米聊等软件主界面的Tab效果。
类似微信主界面,使用tabhost界面切换
字定义TabHost,实现更换tabhost的背景颜色