Android實踐之TabActivity

一、效果

2、主要代碼

 

 

 (1).activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    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">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/tabbar_height"/>
        <TabWidget
            android:id="@android:id/tabs"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:gravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tabbar_height">
            <LinearLayout
                android:id="@+id/ll_first"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent">
                <TextView
                    style="@style/TabButton"
                    android:text="@string/menu_first"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/ll_second"
                android:orientation="vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent">
                <TextView
                    style="@style/TabButton"
                    android:text="@string/menu_second"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
        <LinearLayout
            android:id="@+id/ll_third"
            android:orientation="vertical"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent">
            <TextView
                style="@style/TabButton"
                android:text="@string/menu_third"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</TabHost>

(2)styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="TabButton">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:padding">5dp</item>
        <item name="android:layout_gravity">center</item>
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/weixin_text_three_state</item>
        <item name="android:textSize">12sp</item>
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#000000</item>
    </style>
    <dimen name="tabbar_height">40dp</dimen>
</resources>

(3)strings.xml

<resources>
    <string name="app_name">ToolBar</string>
    <string name="menu_first">首頁</string>
    <string name="menu_second">分類</string>
    <string name="menu_third">購物車</string>
</resources>

(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, Class<?>cls) {
        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給上完整實例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章