Android Tab -- 使用TabWidget、TabHost、TabActivity来实现

原文地址http://blog.csdn.net/crazy1235/article/details/42678877

TabActivity在API13之后被fragment替代了,所以不建议使用

效果:点击头像标签,进行切换。

代码:https://github.com/ldb-github/Layout_Tab

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="false"/>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@android:id/tabs">

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#DEB887"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#BCEE68"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#7D9EC0"
                    android:orientation="vertical">

                </LinearLayout>
            </FrameLayout>
            
        </RelativeLayout>

    </TabHost>

</LinearLayout>
tabhost_tabwidget_tabactivity.xml
public class TabHostTabWidgetTabActivity extends TabActivity {

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

        TabHost tabHost = getTabHost(); //(TabHost) findViewById(android.R.id.tabhost);

        tabHost.addTab(tabHost
                .newTabSpec("111")
                .setIndicator("", getResources().getDrawable(R.drawable.wuyong))
                .setContent(R.id.tab1));

        tabHost.addTab(tabHost
                .newTabSpec("222")
                .setIndicator("", getResources().getDrawable(R.drawable.gongsunsheng))
                .setContent(R.id.tab2));

        tabHost.addTab(tabHost
                .newTabSpec("333")
                .setIndicator("", getResources().getDrawable(R.drawable.likui))
                .setContent(R.id.tab3));

        tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));

        tabHost.setCurrentTab(0);

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                Toast.makeText(TabHostTabWidgetTabActivity.this, tabId, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
TabHostTabWidgetTabActivity.java
原文地址:https://www.cnblogs.com/yarightok/p/5639639.html