Android TabHost如何實現(xiàn)頂部選項卡
更新時間:2020年09月23日 15:38:24 作者:手撕高達的村長
這篇文章主要介紹了Android TabHost如何實現(xiàn)頂部選項卡,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
用TabHost 來實現(xiàn)頂部選項卡,上代碼:activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TabHost android:id="@+id/tabMenu" android:layout_width="match_parent" android:layout_height="0dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <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:orientation="vertical"> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </android.support.constraint.ConstraintLayout>
主方法MainActivity.java
package action.sun.com.tabhost; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TabHost; public class MainActivity extends AppCompatActivity { private TabHost tabhost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到TabHost對象實例 tabhost =(TabHost) findViewById(R.id.tabMenu); //調(diào)用 TabHost.setup() tabhost.setup(); //創(chuàng)建Tab標簽 tabhost.addTab(tabhost.newTabSpec("one").setIndicator("紅色").setContent(R.id.tab1)); tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黃色").setContent(R.id.tab2)); tabhost.addTab(tabhost.newTabSpec("three").setIndicator("黃色").setContent(R.id.tab3)); tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String s) { Log.d("xxx", "onTabChanged: ="+s); if (s.equals("one")){ //可是讓viewpage的視圖顯示出來 //viewPager.setCurrentItem(0); }else if (s.equals("two")){ ////可是讓viewpage的視圖顯示出來 // viewPager.setCurrentItem(1); } } }); } }
實現(xiàn)效果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android開發(fā)之TabHost選項卡及相關(guān)疑難解決方法
- Android TabHost選項卡標簽圖標始終不出現(xiàn)的解決方法
- Android組件TabHost實現(xiàn)頁面中多個選項卡切換效果
- android TabHost(選項卡)的使用方法
- android 選項卡(TabHost)如何放置在屏幕的底部
- Android TabLayout(選項卡布局)簡單用法實例分析
- Android多個TAB選項卡切換效果
- Android實現(xiàn)底部導(dǎo)航欄功能(選項卡)
- Android仿微信底部實現(xiàn)Tab選項卡切換效果
- android選項卡TabHost功能用法詳解
相關(guān)文章
zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的實戰(zhàn)教程
二維碼的應(yīng)用已經(jīng)可以說是非常廣泛了,下面這篇文章主要給大家介紹了關(guān)于zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09Win10下Android App安裝配置開發(fā)環(huán)境
這篇文章主要為大家詳細介紹了Win10下Android App安裝配置開發(fā)環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Android使用 Retrofit 2.X 上傳多文件和多表單示例
本篇文章主要介紹了Android使用 Retrofit 2.X 上傳多文件和多表單示例,具有一定的參考價值,有興趣的小伙伴一起來了解一下2017-08-08