TabActivity去掉下划线

这篇博客介绍了如何通过反射机制,在Android 2.1及更高版本中移除TabActivity的下划线。代码示例展示了针对不同版本系统,设置mBottomLeftStrip和mBottomRightStrip或mLeftStrip和mRightStrip为透明图片来达到效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

		}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值