zoukankan      html  css  js  c++  java
  • 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));

    }

    }

    }});

    }

    }

     

  • 相关阅读:
    秋季总结
    PTA第一个编程题总结
    秋季学习总结
    人生路上对我影响最大的三位老师
    自我介绍
    真实的我——自我介绍
    学习总结
    寒假编程总结三
    寒假编程总结二
    寒假编程总结一
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3588629.html
Copyright © 2011-2022 走看看