TabWidget去除底部下划线

采用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两个变量),从而达到修改。

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版本下运行效果

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值