app选项卡

本文介绍如何使用Android原生TabHost和TabWidget控件实现选项卡功能。通过设置ID及添加切换布局,配合Java代码实现选项卡间的切换效果,并展示了具体的XML布局及Java代码示例。

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

这里实现选项卡是使用android提供的原生的tabHosttabWidget两个控件完成的,在使用的时候,注意在定义id的时候,对于TabHostTabWidget两个控件的Id是不能少的。这里使用的是androidid的形式进行定义id的,这样定义id和使用android:@+id的原理是一样的,只不过在actiivyt中的使用时。一个是使用R.id.xx,这里的R文件就是关于我们所创建的包来命名的,另一个是使用android.R.id.xxx,这种使用表明的是使用android自带的控件。

其次,就是创建相对应的切换布局,这里创建的切换布局都是使用的lIneLayout布局实现的,每种布局都是对应的ListView,这样一共创建了4个,而且每一种都有相对应的id

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:layout_alignParentLeft="true"
>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="32dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="top"
>
        </TabWidget>

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

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

                <ListView
                    android:id="@+id/fishLocalList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
>
                </ListView>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFF7"
                android:orientation="horizontal"
>
                <ListView
                    android:id="@+id/jiakeLocalList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
>
                </ListView>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFF7"
                android:orientation="horizontal"
>

                <ListView
                    android:id="@+id/ruantiLocalList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
>
                </ListView>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFF7"
                android:orientation="horizontal"
>

                <ListView
                    android:id="@+id/zaoleiLocalList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
>
                </ListView>
            </LinearLayout>




        </FrameLayout>
    </LinearLayout>
</TabHost>

1) java代码中实现

TabHost tabHost;
View child1,child2,child3,child4;
ListView fishLocalList,jiakeLocalList,ruantiLocalList,zaoleiLocalList;

public void initData(){
    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    fishLocalList=(ListView)findViewById(R.id.fishLocalList);
    jiakeLocalList=(ListView)findViewById(R.id.jiakeLocalList);
    ruantiLocalList=(ListView)findViewById(R.id.ruantiLocalList);
    zaoleiLocalList=(ListView)findViewById(R.id.zaoleiLocalList);
}

public void initlickFishList(){
    clickList(fishLocalList);
    clickList(jiakeLocalList);
    clickList(ruantiLocalList);
    clickList(zaoleiLocalList);
}

public void initTabXuanxiangka(){

    tabHost.setup();
    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1));
    tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.tab2));
    tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.tab3));
    tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("tab4").setContent(R.id.tab4));

    child1= tabHost.getTabWidget().getChildAt(0);
    child2= tabHost.getTabWidget().getChildAt(1);
    child3= tabHost.getTabWidget().getChildAt(2);
    child4= tabHost.getTabWidget().getChildAt(3);

    initTabWidget(tabHost.getTabWidget(),0,R.drawable.tab_fish_xuanxiangka_drawable);
    initTabWidget(tabHost.getTabWidget(),1,R.drawable.tab_jiake_xuanxiangka_drawable);
    initTabWidget(tabHost.getTabWidget(),2,R.drawable.tab_ruanti_xuanxiangka_drawable);
    initTabWidget(tabHost.getTabWidget(),3,R.drawable.tab_zaolei_xuanxiangka_drawable);
}

public void initTabWidget(TabWidget tabWidget,int index,int drawableResource){
    LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)tabWidget.getChildAt(index).getLayoutParams();
    tabWidget.getChildAt(index).setBackgroundResource(drawableResource);
    lp.width = LinearLayout.LayoutParams.MATCH_PARENT;
    lp.height = 160;
    lp.weight = 0.5f;

    tabWidget.getChildAt(0).setBackgroundResource(R.drawable.fish_on);
    tabWidget.getChildAt(1).setBackgroundResource(R.drawable.jiake_off);
    tabWidget.getChildAt(2).setBackgroundResource(R.drawable.ruanti_off);
    tabWidget.getChildAt(3).setBackgroundResource(R.drawable.zao_off);

    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
            if(tabId.equals("tab1")){
                child1.setBackgroundResource(R.drawable.fish_on);
                child2.setBackgroundResource(R.drawable.jiake_off);
                child3.setBackgroundResource(R.drawable.ruanti_off);
                child4.setBackgroundResource(R.drawable.zao_off);
            }else  if(tabId.equals("tab2")){
                child1.setBackgroundResource(R.drawable.fish_off);
                child2.setBackgroundResource(R.drawable.jiake_on);
                child3.setBackgroundResource(R.drawable.ruanti_off);
                child4.setBackgroundResource(R.drawable.zao_off);
            }else  if(tabId.equals("tab3")){
                child1.setBackgroundResource(R.drawable.fish_off);
                child2.setBackgroundResource(R.drawable.jiake_off);
                child3.setBackgroundResource(R.drawable.ruanti_on);
                child4.setBackgroundResource(R.drawable.zao_off);
            }else  if(tabId.equals("tab4")){
                child1.setBackgroundResource(R.drawable.fish_off);
                child2.setBackgroundResource(R.drawable.jiake_off);
                child3.setBackgroundResource(R.drawable.ruanti_off);
                child4.setBackgroundResource(R.drawable.zao_on);
            }
        }
    });
}

2) 注意点

在实现选项卡的时候,一定要注意要给TabHostTabWidget加上id,而且要加的id都是必须的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值