zoukankan      html  css  js  c++  java
  • Android自定义Tabs文字,背景

    public class HomeActivity extends TabActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature ( Window.FEATURE_NO_TITLE );     //去除Title
            setContentView(R.layout.activity_home);
            final TabHost dh =getTabHost(); //获得TabHost实例
            final TabWidget tabWidget = dh.getTabWidget();//获得TabWidget实例
            dh.addTab(dh.newTabSpec("A").setIndicator(getResources().getString(R.string.tab1)).setContent(new Intent(this,FirstActivity.class)));
            dh.addTab(dh.newTabSpec("B").setIndicator(getResources().getString(R.string.tab2)).setContent(new Intent(this,SecondActivity.class)));
            for (int i =0; i < tabWidget.getChildCount(); i++) {
              View child = tabWidget.getChildAt(i);
              final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
              tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
              tv.setTextSize(10);
              RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();
              params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0); //取消文字底边对齐
              params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); //设置文字居中对齐
              child.getLayoutParams().height = 30;  
              
                  if(dh.getCurrentTab()==i){
                          child.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected));
                  }
                  else {
                          child.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected));
                  }
             }  
    //设置点击切换背景图        
    dh.setOnTabChangedListener(new OnTabChangeListener(){
                    public void onTabChanged(String tabId) {
                     // TODO Auto-generated method stub
                     for (int i =0; i < tabWidget.getChildCount(); i++) {
                      View vvv = tabWidget.getChildAt(i);
                      if(dh.getCurrentTab()==i){
                              vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected));
                      }
                      else {
                              vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected));
                      }
                     }
                    }});
        }
  • 相关阅读:
    Maven安装教程详解
    eclipse配置apache tomcat运行时访问路径不需要项目名称
    Spring MVC Captcha 验证码
    Vue混入的详解
    Eclipse上传项目到Git
    mysql 8.0.11 中使用 grant ... identified by 时 error 1064 near 'identified by '密码'' at line 1
    转 js自定义事件——Event和CustomEvent
    tp隐藏入口文件
    php发送邮件
    【项目练习】控制器直接操作--登录
  • 原文地址:https://www.cnblogs.com/seantown/p/2810034.html
Copyright © 2011-2022 走看看