TabWidget去除底部下划线_横线

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
TabHost 是 Android 中的一个容器,用于实现多个标签页之间的切换。下面是 TabHost 的使用方法详解: 1. 在 XML 布局文件中添加 TabHost 控件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <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="0dp" android:layout_weight="1" /> </LinearLayout> </TabHost> ``` 2. 在 Activity 中初始化 TabHost: ```java TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 添加标签页: ```java TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Tab1"); tabSpec1.setIndicator("Tab 1"); tabSpec1.setContent(R.id.tab1_content); TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Tab2"); tabSpec2.setIndicator("Tab 2"); tabSpec2.setContent(R.id.tab2_content); tabHost.addTab(tabSpec1); tabHost.addTab(tabSpec2); ``` 4. 创建标签页的布局: 在布局文件中创建用于显示每个标签页内容的 View,例如: ```xml <LinearLayout android:id="@+id/tab1_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 1 的内容 --> </LinearLayout> <LinearLayout android:id="@+id/tab2_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 2 的内容 --> </LinearLayout> ``` 以上就是 TabHost 的基本使用方法。通过添加不同的标签页和对应的布局,可以实现多个标签页之间的切换和显示不同的内容。你可以根据自己的需求来定制标签页的样式和内容。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值