Android自定义TAB选项卡



import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

/**
 * @Description:
 * @ChangeList:
 */
public class MyTabActivity extends TabActivity {

    private static final String TAG = "MyTabActivity";
    private static final int TAB_HEIGHT = 40;
    private TabHost tabHost;
    private TabWidget tabWidget;

    @Override
    protected void onStart() {
        Log.d(TAG, FileUtils.getMethodName());
        super.onStart();
    }

    @Override
    protected void onRestart() {
        Log.d(TAG, FileUtils.getMethodName());
        super.onRestart();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, FileUtils.getMethodName());
        super.onStop();
    }

    @Override
    protected void onPause() {
        Log.d(TAG, FileUtils.getMethodName());
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, FileUtils.getMethodName());
        super.onDestroy();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, FileUtils.getMethodName());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index_tab);
        tabHost = this.getTabHost();
        tabHost.getTabWidget().setStripEnabled(false);

        TabSpec ts1 = tabHost.newTabSpec("1");
        ts1.setIndicator(getString(R.string.title_sdcard));
        ts1.setContent(new Intent(this, A.class));
        tabHost.addTab(ts1);

        TabSpec ts2 = tabHost.newTabSpec("2");
        ts2.setIndicator(getString(R.string.title_remote));
        ts2.setContent(new Intent(this, B.class));
        tabHost.addTab(ts2);

        tabWidget = (TabWidget) findViewById(android.R.id.tabs);

        for (int i = 0; i < tabWidget.getChildCount(); i++) {
            View view = tabWidget.getChildAt(i);
            view.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.grey));
            view.getLayoutParams().height = TAB_HEIGHT;

            TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
                    android.R.id.title);
            tv.setTextColor(Color.WHITE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv
                    .getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
            params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            tv.setLayoutParams(params);
        }

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                for (int i = 0; i < tabWidget.getChildCount(); i++) {
                    View v = tabWidget.getChildAt(i);
                    if (tabHost.getCurrentTab() == i) {
                        v.setBackgroundDrawable(getResources().getDrawable(
                                R.drawable.tab_bg1));
                    } else {
                        v.setBackgroundDrawable(getResources().getDrawable(
                                R.drawable.tab_bg2));
                    }
                }
            }
        });
        tabHost.setCurrentTab(0);
        tabWidget.getChildAt(0).setBackgroundDrawable(
                getResources().getDrawable(R.drawable.tab_bg1));
    }
}

 

 

 

 

 

 

 

XML

 

 



    
        
            
            
        
    

 

 

 

以后补充...

你可能感兴趣的:(Android)