Android实践之TabActivity

一、效果

Android实践之TabActivity_第1张图片

2、主要代码

 

 

 (1).activity_main.xml




    
        
        
        
            
                
            
            
                
            
        
            
        
        
    

(2)styles.xml



    
    
    
    40dp

(3)strings.xml


    ToolBar
    首页
    分类
    购物车

(4)MainActivity.java

package com.example.toolbar;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;

public class MainActivity extends TabActivity implements View.OnClickListener{

    private static final String TAG = "MainActivity";

    private Bundle mBundle = new Bundle();
    private TabHost tab_host;
    private LinearLayout ll_first, ll_second, ll_third;
    private String FIRST_TAG = "first";
    private String SECOND_TAG = "second";
    private String THIRD_TAG = "third";

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

        mBundle.putString("tag", TAG);
        ll_first = findViewById(R.id.ll_first);
        ll_second = findViewById(R.id.ll_second);
        ll_third = findViewById(R.id.ll_third);

        ll_first.setOnClickListener(this);
        ll_second.setOnClickListener(this);
        ll_third.setOnClickListener(this);

        tab_host = getTabHost();

        tab_host.addTab(getNewTab(FIRST_TAG, R.string.menu_first, R.drawable.ic_launcher_background, Main2Activity.class));
        tab_host.addTab(getNewTab(SECOND_TAG, R.string.menu_second, R.drawable.ic_launcher_background, Main3Activity.class));
        tab_host.addTab(getNewTab(THIRD_TAG, R.string.menu_third, R.drawable.ic_launcher_background, Main4Activity.class));
        changeContainerView(ll_first);
    }

    private TabHost.TabSpec getNewTab(String spec, int label, int icon, Classcls) {
        Intent intent = new Intent(this, cls).putExtras(mBundle);
        return tab_host.newTabSpec(spec).setContent(intent).setIndicator(getString(label), getResources().getDrawable(icon));
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_first:
            case R.id.ll_second:
            case R.id.ll_third:
                changeContainerView(view);
        }
    }

    private void changeContainerView(View v) {
        ll_first.setSelected(false);
        ll_second.setSelected(false);
        ll_third.setSelected(false);
        v.setSelected(true);
        if (v == ll_first) {
            tab_host.setCurrentTabByTag(FIRST_TAG);
        } else if (v == ll_second) {
            tab_host.setCurrentTabByTag(SECOND_TAG);
        } else if (v == ll_third) {
            tab_host.setCurrentTabByTag(THIRD_TAG);
        }
    }
}

3、资源链接

如果还不清楚,https://download.csdn.net/download/chicaidecaiji/12409083给上完整实例

你可能感兴趣的:(Android篇,Android,Studio开发实战练习)