`
rafale35
  • 浏览: 20032 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TabWidget
                android:id="@android:id/tabs"
                android:background="@null"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>
    </TabHost>
</LinearLayout>

 

 

 

以后补充...

分享到:
评论
1 楼 llty 2011-10-28  
有帮助 !  以后写博客 能上图 和 源码吗!? 

相关推荐

Global site tag (gtag.js) - Google Analytics