Android中利用TabActivity制作首页框架

今天我写的这个知识点也是一个标签页的做法,只不过和以前的不太一样。我们平时做tab页,需要一个依赖的Activity,然后内嵌多个Fragment实现标签页面。那你知道如何把Activity嵌入进去,变成Tab页吗?今天我们一起了解一下TabActivity

首先我们要有一个布局

<?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:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent"
            android:layout_marginBottom="10dp"/>

        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"
            android:visibility="gone"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_alignParentBottom="true"
            android:gravity="bottom"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tv1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="首页"
                android:gravity="center_horizontal"
                android:drawableTop="@mipmap/ic_message_choose"/>

            <TextView
                android:id="@+id/tv2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="精选"
                android:gravity="center_horizontal"
                android:drawableTop="@mipmap/ic_message_choose"/>

            <TextView
                android:id="@+id/tv3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="消息"
                android:gravity="center_horizontal"
                android:drawableTop="@mipmap/ic_message_choose"/>

            <TextView
                android:id="@+id/tv4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="我的"
                android:gravity="center_horizontal"
                android:drawableTop="@mipmap/ic_message_choose"/>
        </LinearLayout>
    </RelativeLayout>
</TabHost>

有几个核心

  • 根节点是TabHost以及这个id,必须有
  • 要有一个FrameLayout,承载加载的页面,id必须有
  • 要有TabWidge,id必须有

这几个要点都有了,下面的按钮就随意一些了,各种实现方法均可。

接着是Activity

public class MainActivity extends TabActivity implements View.OnClickListener{

    private static final String TAG = "TabActivity";
    private Bundle mBundle = new Bundle();
    private TabHost tabHost;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tabHost = getTabHost();
        tabHost.addTab(getNewTab("tv1",R.string.menu_one,R.mipmap.ic_message_choose,FirstActivity.class));
        tabHost.addTab(getNewTab("tv2",R.string.menu_two,R.mipmap.ic_message_choose,SecondActivity.class));
        tabHost.addTab(getNewTab("tv3",R.string.menu_three,R.mipmap.ic_message_choose,ThreadActivity.class));
        tabHost.addTab(getNewTab("tv4",R.string.menu_four,R.mipmap.ic_message_choose,FourActivity.class));
        tabHost.setCurrentTabByTag("tv1");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv1:
                tabHost.setCurrentTabByTag("tv1");
                break;
            case R.id.tv2:
                tabHost.setCurrentTabByTag("tv2");
                break;
            case R.id.tv3:
                tabHost.setCurrentTabByTag("tv3");
                break;
            case R.id.tv4:
                tabHost.setCurrentTabByTag("tv4");
                break;
        }
    }

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

Activity也有几个核心

  • 继承TabActivity
  • 几个嵌入的Activity要在清单文件中配置
  • getNewTab的第一个参数是一个tag,我们是根据这个tag进行页面切换的,可以进行统一的管理。

这样我们就可以把多个Activity页面加载到同一个Activity中了,交互可以考虑使用接口回调的方式。

不过更多的我们推荐使用Activity+Fragment的实现方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值