自己用TabHost的记录

本文详细介绍了如何使用TabHost在Android应用中创建Tab栏,包括使用TabActivity、setIndicator方法设置指示器、setContent方法跳转界面以及添加不同Tab的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TabHost的用法之一是用一个activity来继承TabActivity

下面看一下具体的代码:

MainActivity.java

下面的

setIndicator方法可以接受一个String类型的字符串,也可以接受一个view,比如可以传TabHost的layout布局;

setContent()方法是点按这个Tab的时候需要跳转到界面,可以new一个intent,比如:tab1.setContent(new Intent(this, com.example.tabhost.TabActivity.class));或者直接像代码中所写。直接给传一个主界面layout的一个控件id;

tabs.newTabSpec("tab1")方法是用来区分到底创建的是哪个Tab,其中给传的字符串就是用来区分的,这个字符串可以传任意的字符串

package com.example.tabhost;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

    private TabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mTabHost = getTabHost();
//        LayoutInflater.from(this).inflate(R.layout.activity_main,
//                tabs.getTabContentView(), true);
        add1();
        add2();
        add3();
        add4();

//        ImageView iv = (ImageView) tabStyle.findViewById(R.id.icon);
//        TextView tv = (TextView) tabStyle.findViewById(R.id.title);
        //mTabHost.getTabWidget().getChildCount()是获取tab的总个数,然后获取每个tab布局中的imageview和textview,
        //分别给imageview添加图片和给textview添加标题
        for(int i = 0;i < mTabHost.getTabWidget().getChildCount();i++){
            LinearLayout tabStyle2 = (LinearLayout)LayoutInflater.from(this)
                  .inflate(R.layout.tab_style_people, null);
            ImageView iv = (ImageView)mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.icon);
            TextView tv = (TextView)mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.title);
            Drawable drawable = null;
            String title = null;
            switch (i) {
            case 0:
                drawable = getResources().getDrawable(R.drawable.ic_ab_dialer_holo_light);
                title = getResources().getString(R.string.callLogIconLabel);
                break;
            case 1:
                drawable = getResources().getDrawable(R.drawable.ic_ab_favourites_holo_dark);
                title = getResources().getString(R.string.contactsFavoritesLabel);
                break;
            case 2:
                drawable = getResources().getDrawable(R.drawable.ic_ab_history_holo_dark);
                title = getResources().getString(R.string.contactsGroupsLabel);
                break;
            case 3:
                drawable = getResources().getDrawable(R.drawable.ic_ab_search_holo_dark);
                title = getResources().getString(R.string.people);
                break;
            default:
                break;
                
            }
            //将对应的图片和对应的标题添加到对应的控件中
            iv.setImageDrawable(drawable);
            tv.setText(title);
        }
    }
    //添加第一个Tab
    private void add1() {
        LinearLayout tabStyle = (LinearLayout) LayoutInflater.from(this)
                .inflate(R.layout.tab_style_people, null);
        TabSpec tab1 = mTabHost.newTabSpec("tab1");
        tab1.setIndicator(getResources().getString(R.string.callLogIconLabel));
        tab1.setContent(new Intent(this, com.example.tabhost.TabActivity.class));
        mTabHost.addTab(tab1);
        //这里的setIndicator方法可以接受一个String类型的字符串,也可以接受一个view,比如可以传TabHost的layout布局
    }
    //添加第二个Tab
    private void add2() {
        LinearLayout tabStyle = (LinearLayout) LayoutInflater.from(this)
                .inflate(R.layout.tab_style_people, null);
        TabSpec tab2 = mTabHost.newTabSpec("tab2");
        tab2.setIndicator(getResources().getString(R.string.contactsFavoritesLabel));
        tab2.setContent(new Intent(this, com.example.tabhost.TabActivity.class));
        mTabHost.addTab(tab2);
    }
    //添加第三个Tab
    private void add3() {
        LinearLayout tabStyle = (LinearLayout) LayoutInflater.from(this)
                .inflate(R.layout.tab_style_people, null);
        TabSpec tab3 = mTabHost.newTabSpec("tab3");
        tab3.setIndicator(getResources().getString(R.string.people));
        tab3.setContent(new Intent(this, com.example.tabhost.TabActivity.class));
        mTabHost.addTab(tab3);
    }
    //添加第四个Tab
    private void add4() {
        LinearLayout tabStyle = (LinearLayout) LayoutInflater.from(this)
                .inflate(R.layout.tab_style_people, null);
        TabSpec tab4 = mTabHost.newTabSpec("tab4");
        tab4.setIndicator(getResources().getString(R.string.people));
        tab4.setContent(new Intent(this, com.example.tabhost.TabActivity.class));
        mTabHost.addTab(tab4);
    }

}


main.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"    
        >  
         
       <LinearLayout  
        android:id="@+id/Tab1"  
            android:layout_width="wrap_content"  
            android:layout_height="fill_parent"  
            android:orientation="vertical"  
        >  
                      
        <TextView   
            android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:text="这是tab1"  
            />  
        </LinearLayout>  
          
           <LinearLayout  
            android:id="@+id/Tab2"  
            android:layout_width="wrap_content"  
            android:layout_height="fill_parent"  
            android:orientation="vertical"  
         >  
                      
            <TextView   
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:text="这是tab2"  
            />  
        </LinearLayout>  
    </FrameLayout>  

table_style.xml
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dip"
    android:paddingRight="5dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="12sp"
        android:textColor="#000000" />

</LinearLayout>









资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 华为移动服务(Huawei Mobile Services,简称 HMS)是一个全面开放的移动服务生态系统,为企业和开发者提供了丰富的工具和 API,助力他们构建、运营和推广应用。其中,HMS Scankit 是华为推出的一款扫描服务 SDK,支持快速集成到安卓应用中,能够提供高效且稳定的二维码和条形码扫描功能,适用于商品扫码、支付验证、信息获取等多种场景。 集成 HMS Scankit SDK 主要包括以下步骤:首先,在项目的 build.gradle 文件中添加 HMS Core 库和 Scankit 依赖;其次,在 AndroidManifest.xml 文件中添加相机访问和互联网访问权限;然后,在应用程序的 onCreate 方法中调用 HmsClient 进行初始化;接着,可以选择自定义扫描界面或使用 Scankit 提供的默认扫描界面;最后,实现 ScanCallback 接口以处理扫描成功和失败的回调。 HMS Scankit 内部集成了开源的 Zxing(Zebra Crossing)库,这是一个功能强大的条码和二维码处理库,提供了解码、生成、解析等多种功能,既可以单独使用,也可以与其他扫描框架结合使用。在 HMS Scankit 中,Zxing 经过优化,以更好地适应华为设备,从而提升扫描性能。 通常,ScanKitDemoGuide 包含了集成 HMS Scankit 的示例代码,涵盖扫描界面的布局、扫描操作的启动和停止以及扫描结果的处理等内容。开发者可以参考这些代码,快速掌握在自己的应用中实现扫码功能的方法。例如,启动扫描的方法如下: 处理扫描结果的回调如下: HMS Scankit 支持所有安卓手机,但在华为设备上能够提供最佳性能和体验,因为它针对华为硬件进行了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值