• 自定义TAB


    package com.testTabActivity;

    import java.lang.reflect.Field;
    import android.app.Activity;
    import android.app.TabActivity;
    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 testTabActivity extends TabActivity {
        
    /** Called when the activity is first created. */
        @Override
        
    public void onCreate(Bundle savedInstanceState) {
            
    super.onCreate(savedInstanceState);

            
    int width =45;
            
    int height =48;

            
    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(
    "news",getResources().getDrawable(R.drawable.efg))
                 .setContent(R.id.widget_layout_Blue));

            tabs.addTab(tabs.newTabSpec(
    "second tab")
             .setIndicator(
    "help",getResources().getDrawable(R.drawable.c))
             .setContent(R.id.widget_layout_green));

            tabs.addTab(tabs.newTabSpec(
    "second tab")
                 .setIndicator(
    "setting",getResources().getDrawable(R.drawable.e))
                 .setContent(R.id.widget_layout_red));



            
    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的接口是怎么样的,现在先加个判断好一些
                 
    */
          
    if (Float.valueOf(Build.VERSION.RELEASE) <= 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.linee));
                     mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.linee));

                   } 
    catch (Exception e) {
                     e.printStackTrace();
                   }
             } 
    else {
            
             }
            View vvv 
    = tabWidget.getChildAt(i);
      
    if(tabs.getCurrentTab()==i){
              vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.shine));
      }
      
    else {
              vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.seven));
      }

            }
            
    /**
             * 当点击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.shine));
         }
         
    else {
                 vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.seven));
         }
        }
       }});

        }

    }
  • 相关阅读:
    Linux下mysql使用systemctl restart mysqld命令失败
    Linux环境下mysql报错:bash: mysql: command not found 的解决方法
    Linux查看mysql是否启动的命令
    启动MySQL5.7时报错:initialize specified but the data directory has files in it. Aborting.
    ARM64架构下面安装mysql5.7.22
    Python3.6打开EAIDK-610开发板(计算机通用)摄像头拍照并保存
    Python的几种主动结束程序方式
    aarch64架构下安装tensorflow详细过程
    python代码在linux终端中执行报错:Unable to init server: Could not connect: Connection refused
    red hat 报错:apt-get:找不到命令
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1744378.html
Copyright © 2020-2023  润新知