• Android开发之自己定义TabHost文字及背景(源码分享)


        使用TabHost 能够在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该怎样进行自己定义改动优化呢

    MainActivity的源码

    package com.dream.ledong;
    
    import android.app.TabActivity;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    
    import android.view.Gravity;
    import android.widget.RelativeLayout;
    import android.widget.TabHost;
    import android.widget.TabHost.OnTabChangeListener;
    import android.widget.TabWidget;
    import android.widget.TextView;
    
    import com.example.client.R;
    
    @SuppressWarnings("deprecation")
    public class itemList extends TabActivity {
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.itemlist);
    		final TabHost tabHost = getTabHost();
    		Intent remoteIntent = new Intent(itemList.this, item1.class);
    		TabHost.TabSpec remoteTabSpec = tabHost.newTabSpec("remote");
    		remoteTabSpec.setIndicator("运动推荐");
    		remoteTabSpec.setContent(remoteIntent);
    		tabHost.addTab(remoteTabSpec);
    		
    		Intent localIntent = new Intent(itemList.this, item2.class);
    		TabHost.TabSpec localTabSpec = tabHost.newTabSpec("local");
    		localTabSpec.setIndicator("球友人气");
    		localTabSpec.setContent(localIntent);
    		tabHost.addTab(localTabSpec);
    		
    		Intent localIntent2 = new Intent(itemList.this, item2.class);
    		TabHost.TabSpec localTabSpec2 = tabHost.newTabSpec("a");
    		localTabSpec2.setIndicator("竞技氛围");
    		localTabSpec2.setContent(localIntent2);
    		tabHost.addTab(localTabSpec2);
            
    		updateTabStyle(tabHost);
    
    		// 当某个Tab被选中时,则更新背景样式
    		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    			@Override
    			public void onTabChanged(String tabId) {
    				updateTabStyle(tabHost);
    			}
    		});
    	}
    
    	private void updateTabStyle(final TabHost mTabHost) {
    		TabWidget tabWidget = mTabHost.getTabWidget();
    		tabWidget.setRightStripDrawable(R.drawable.list_item_divide_operate);
    		tabWidget.setLeftStripDrawable(R.drawable.list_item_divide_operate);
    		for (int i = 0; i < tabWidget.getChildCount(); i++) {
    			RelativeLayout tabView = (RelativeLayout) mTabHost.getTabWidget()
    					.getChildAt(i);
    			TextView text = (TextView) tabWidget.getChildAt(i).findViewById(
    					android.R.id.title);
    			text.setTextSize(15);
    			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text
    					.getLayoutParams();
    			params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    			params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
    			text.setLayoutParams(params); 
    			text.setGravity(Gravity.CENTER);
    			if (mTabHost.getCurrentTab() == i) {
    				// 选中
    				tabView.setBackgroundColor(Color.parseColor("#8DB6CD"));
    				text.setTextColor(this.getResources().getColorStateList(
    						android.R.color.black));
    			} else {
    				// 未选中
    				tabView.setBackgroundColor(Color.parseColor("#ffffff"));
    				text.setTextColor(this.getResources().getColorStateList(
    						android.R.color.darker_gray));
    			}
    		}
    	}
    
    }


  • 相关阅读:
    动手动脑(类与对象作业再次提交)
    论团队(类与对象邮箱作业再次提交)
    流于形式的沟通
    加密
    string类中一些方法的使用
    StringEquals的用法
    命令行接收数字求和
    计算机思维
    SpringBoot之Callable处理异步请求
    MySQL8.0 zip版本 安装
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3753018.html
Copyright © 2020-2023  润新知