仿新浪微博TAB布局


最近研究新浪微博TAB布局,反编译新浪微博布局代码,并借鉴网上其他的实现方式,终于弄明白了其中的原理。图片资源素材全部取自新浪微博客户端资源文件,仅作研究之需,如有冒犯之处还请多多包涵!


实现原理:使用RadioGroup 实现底部TAB布局


效果图

仿新浪微博TAB布局_第1张图片



仿新浪微博TAB布局_第2张图片



仿新浪微博TAB布局_第3张图片



maintabs.xml




    

        

            
            

            

            

                

                    

                    

                    

                    

                    
                
            
        
    


icon_home.xml



    
    



maintabt样式



MainTabActivity.java

package com.example.tabbottom;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

public class MainTabActivity extends TabActivity implements OnCheckedChangeListener {

	private RadioGroup radioGroup;
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintabs);
		
		findView();
	}

	private void findView() {
		
		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
		
		tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("home").setIndicator("Home").setContent(new Intent(this, HomeActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("msg").setIndicator("Msg").setContent(new Intent(this, MessageActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("profile").setIndicator("Profile").setContent(new Intent(this, ProfileActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("square").setIndicator("Square").setContent(new Intent(this, SquareActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("more").setIndicator("More").setContent(new Intent(this, MoreActivity.class)));
        
        ((RadioButton) findViewById(R.id.radio_home)).setChecked(true);
        
        radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(this);
       
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {

		switch (checkedId) {
		case R.id.radio_home:
			tabHost.setCurrentTabByTag("home");
			break;
		case R.id.radio_msg:
			tabHost.setCurrentTabByTag("msg");
			break;
		case R.id.radio_profile:
			tabHost.setCurrentTabByTag("profile");
			break;
		case R.id.radio_square:
			tabHost.setCurrentTabByTag("square");
			break;
		case R.id.radio_more:
			tabHost.setCurrentTabByTag("more");
			break;
		}
	}

}


HomeActivity.java

package com.example.tabbottom;

import android.app.Activity;
import android.os.Bundle;

public class HomeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.home);
		super.onCreate(savedInstanceState);
	}
}


home.xml



    
    




工程下载地址:http://download.csdn.net/detail/fx_sky/6385721





你可能感兴趣的:(仿新浪微博TAB,tabhost,Android)