Android TabHost 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));
}
}
}});
}
}
Android TabHost TabWidget 去除黑线(底部下划线)的更多相关文章
- Android EditText如何去除边框添加下划线
(一)问题 之前的自定义EditText只能显示高度不超过屏幕高度的文本内容,继续增加内容会出现如下问题: (二)原因分析 下部(超出屏幕高度的部分)没有继续画线,也就是说横线没有画够,那么一定是循环 ...
- android实现对导航Tab设置下划线选中效果
技术人员核心竞争力还是技术啊.努力提高各种实现效果.加油哦! 直接看效果.此linearLayout只有两个Button ,当选中Button1,Button1有个下划线选中效果.当选中Buton2, ...
- 关于android中EditText边框的问题 下划线
方法1 将edittext的style设置成?android:attr/textViewStyle 取消掉默认的样式,在设置background为@null 接下来就是一个空空的edittext了, ...
- android TextView 例子代码(文字中划线、文字下划线)
XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- CSS超链接样式,去除下划线等
控制超链接样式 链接的四种状态: a:link - 普通的.未被访问的链接 a:visited - 用户已访问的链接 a:hover - 鼠标指针位于链接的上方 a:active - 链接被点击的时刻 ...
- Button的几种常用的xml背景,扁平化,下划线,边框包裹,以及按压效果
Button的几种常用的xml背景,扁平化,下划线,边框包裹,以及按压效果 分享下我项目中用到的几种Button的效果,说实话,还真挺好看的 一.标准圆角 效果是这样的 他的实现很简单,我们只需要两个 ...
- React-Native 之 GD (二十)removeClippedSubviews / modal放置的顺序 / Android 加载git图\动图 / 去除 Android 中输入框的下划线 / navigationBar
1.removeClippedSubviews 用于提升大列表的滚动性能.需要给行容器添加样式overflow:’hidden’.(Android已默认添加此样式)此属性默认开启 这个属性是因为在早期 ...
- android TextView 添加下划线
android Textview加下划线 由于新做的一个项目要求有字体带下划线效果,当时看了下其实可以通过图片伪造出那种视觉效果.但是为了体现点技术含量,于是我想用Textview带下划线的效果.方法 ...
- Android TextView中实现点击文本超链接(无下划线)的封装类
android中有的时候须要在TextView上设置一些超链接,点击这些超链接时进行一些操作.比如新浪微博上的一些keyword,点击时会跳转到对应的页面. 怎样实现我们就直接看源代码吧. /** * ...
随机推荐
- 判断String为空
1. et_string.equals("")|| et_string==null 2. ""用equal.null用== TextUtils.isEmpt ...
- JDK版本1.6和6.0到底指什么
Both version numbers (1.6.0 and 6) are used to identify this release of the Java Platform. Version 6 ...
- (四)学习JavaScript之className属性
参考:http://www.w3school.com.cn/jsref/prop_classname.asp HTML DOM Anchor 对象 定义和用法 className 属性设置或返回元素的 ...
- WCF:为 SharePoint 2010 Business Connectivity Services 构建 WCF Web 服务(第 1 部分,共 4 部分)
转:http://msdn.microsoft.com/zh-cn/library/gg318615.aspx 摘要:通过此系列文章(共四部分)了解如何在 Microsoft SharePoint F ...
- sys.argv[]基本用法
#python 3.4.2 #windows系统下可以在CMD下执行 python test.py 第一参数 第2个参数 #空格分隔每个参数,sys.argv[]是用来获取命令行参数的 #引用必要的包 ...
- 第四章:ARP 地址解析协议
网络接口有一个硬件地址,48bit的值,在硬件层次上进行的数据帧交换必须有正确的接口地址.tcp/ip有自己的地址,32bit的IP地址. 但是知道主机的IP地址并不能让内核发送一帧数据给主机.内核( ...
- UVALIVE 3026 Period
题意:给你一个字符串,问第i位前是否有循环节,若存在,则循环节是多少? 思路:考察失配函数f[i]的意义.只要i%(i-f[i])==0,则循环节长度为i/(i-f[i]).字符在[0,f[i]],[ ...
- 【组队训练】2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest
好多oj都崩掉了,于是打了cf.. 开始开的最后一题...尼玛题好长终于看完了...神题不会.... I过了好多人..看了下,一眼题...随便敲了下,1A ]; int main(){ int n, ...
- Java持久化存储对象Properties的方法list、store、load
package FileDemo; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStrea ...
- hdu 1597 find the nth digit
find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...