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


  • 相关阅读:
    NIO与普通IO文件读写性能对比
    JAVA学习.java.sql.date 与java.util.date以及gettime()方法的分析
    软件工程之软件设计
    ubuntu下管理android手机
    AFNetworking2.0 NSHipster翻译
    【Jsoup爬取网页内容】
    IOS 表视图UITableView 束NSBundle
    如何将位图格式图片文件(.bmp)生成geotiff格式图片?
    opencv3 使用glob遍历并修改文件名
    Ubuntu clion下载及激活
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3753018.html
Copyright © 2011-2022 走看看