TabWidget去除底部下划线
2012-04-20 16:24
375 查看
采用TabHost布局时,往往会发现默认的系统风格与软件风格很不协调,比如TabWidget的下划线影响布局效果。通常情况下会去除其下划线。如果是采用xml布局文件,在TabWidget的属性项设置android:tabStripEnabled=”false”(经测试发现,这个属性是在2.2版本以上才能使用),这样能达到去除下划线的目的。
但是如果使用代码去除下划线,情况就比较复杂,就需要根据不同的版本去除下划线。如果是2.2,2.3版本,去除下划线只需要调用一个方法:tabWidget.setStripEnabled(false);就可以去除下划线。但是在2.1及以下版本,就无法去除下划线,因为2.1没有tabWidget.setStripEnabled(boolean b)这个方法。这个时候,可以分析android
2.1,2.2,2.3关于TabWidget的源码,采用反射修改私有的mBottomLeftStrip,mBottomRightStrip两个变量(2.2,2.3是mLeftStrip,mRightStrip两个变量),从而达到修改。
以下是2.1版本的项目在分别运行在2.1,2.3版本上运行的效果图。
图-1 tabhost在2.1版本下运行效果(上面有立体感)
图-2 tabhost在2.3版本下运行效果
但是如果使用代码去除下划线,情况就比较复杂,就需要根据不同的版本去除下划线。如果是2.2,2.3版本,去除下划线只需要调用一个方法:tabWidget.setStripEnabled(false);就可以去除下划线。但是在2.1及以下版本,就无法去除下划线,因为2.1没有tabWidget.setStripEnabled(boolean b)这个方法。这个时候,可以分析android
2.1,2.2,2.3关于TabWidget的源码,采用反射修改私有的mBottomLeftStrip,mBottomRightStrip两个变量(2.2,2.3是mLeftStrip,mRightStrip两个变量),从而达到修改。
package com.test.main; import java.lang.reflect.Field; import android.app.TabActivity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.TabHost; import android.widget.TabWidget; import android.widget.TextView; import android.widget.TabHost.OnTabChangeListener; public class TabhostActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); int width =45; int height =68; final TabHost tabs = getTabHost(); final TabWidget tabWidget = tabs.getTabWidget(); Field mBottomLeftStrip; Field mBottomRightStrip; LayoutInflater.from(this).inflate(R.layout.main, tabs.getTabContentView(), true); tabs.addTab(tabs.newTabSpec("first tab") .setIndicator("信息", getResources().getDrawable(android.R.drawable.ic_dialog_email)) .setContent(new Intent(TabhostActivity.this,OneActivity.class)) ); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("收藏",getResources().getDrawable(android.R.drawable.ic_menu_add)) .setContent(new Intent(TabhostActivity.this,TwoActivity.class))); tabs.addTab(tabs.newTabSpec("third tab") .setIndicator("摄影",getResources().getDrawable(android.R.drawable.ic_menu_camera)) .setContent(new Intent(TabhostActivity.this,ThreeActivity.class))); tabs.setCurrentTab(0); 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,2.3的接口不同,加个判断 */ if (Float.valueOf(Build.VERSION.RELEASE.substring(0, 3)) <= 2.1) { 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 { //如果是2.2,2.3版本开发,可以使用一下方法tabWidget.setStripEnabled(false) //tabWidget.setStripEnabled(false); //但是很可能你开发的android应用是2.1版本, //tabWidget.setStripEnabled(false)编译器是无法识别而报错的,这时仍然可以使用上面的 //反射实现,但是需要修改代码 try { //2.2,2.3接口是mLeftStrip,mRightStrip两个变量,当然代码与上面部分重复了 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) { e.printStackTrace(); } } View vvv = tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.selected)); } else { vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.green)); } } /** * 当点击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.selected)); } else { vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.green)); } } }}); } }
以下是2.1版本的项目在分别运行在2.1,2.3版本上运行的效果图。
图-1 tabhost在2.1版本下运行效果(上面有立体感)
图-2 tabhost在2.3版本下运行效果
相关文章推荐
- TabWidget去除底部下划线_横线
- Android TabHost TabWidget 去除黑线(底部下划线)
- tabWidget去除系统默认白色底角
- 底部tabhost研究三 (TabWidget ,FrameLayout 在TabHost内。TabWidget 和FrameLayout 平行的关系)
- android底部菜单选项卡TabWidget用法
- Android TabWidget底部显示
- Android TabWidget底部显示效果
- android 让底部的Tabwidget透明
- tabhost 底部 颜色1.你想知道tabwidget控件tab标签栏置底? #2.修改tab默认的样式和颜色
- 如何去除TabHost中TabWidget的横线的问题
- Android TabWidget底部显示
- android:使TabWidget在底部
- 实现方式二:隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏
- 利用TabWidget实现底部菜单
- TabHost+TabWidget+FrameLayout实现底部菜单页面
- 使用选项卡TabWidget与TabHost实现底部或者顶部四个分类按钮 点击切换对应的fragment
- RadioButton使用(一)实现底部带指示条的Tab选项卡
- 去除html超链接中的下划线
- iphone 微信下浏览器中数字去除下划线
- Android TabHost,TabWidget选项卡总结