Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡
用TabHost 來實(shí)現(xiàn)頂部選項(xiàng)卡,上代碼: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對象實(shí)例
tabhost =(TabHost) findViewById(R.id.tabMenu);
//調(diào)用 TabHost.setup()
tabhost.setup();
//創(chuàng)建Tab標(biāo)簽
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);
}
}
});
}
}
實(shí)現(xiàn)效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法
- Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
- Android組件TabHost實(shí)現(xiàn)頁面中多個選項(xiàng)卡切換效果
- android TabHost(選項(xiàng)卡)的使用方法
- android 選項(xiàng)卡(TabHost)如何放置在屏幕的底部
- Android TabLayout(選項(xiàng)卡布局)簡單用法實(shí)例分析
- Android多個TAB選項(xiàng)卡切換效果
- Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
- Android仿微信底部實(shí)現(xiàn)Tab選項(xiàng)卡切換效果
- android選項(xiàng)卡TabHost功能用法詳解
相關(guān)文章
Android左滑返回功能的實(shí)現(xiàn)示例代碼
本篇文章主要介紹了Android左滑返回的實(shí)現(xiàn)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
安卓(Android)聊天機(jī)器人實(shí)現(xiàn)代碼分享
這是一個安卓智能聊天機(jī)器人的源碼,采用了仿微信的風(fēng)格設(shè)計(jì),調(diào)用的是圖靈機(jī)器人的API,能夠?qū)崿F(xiàn)智能聊天、講故事、講笑話、查天氣、查公交等豐富的功能2015-11-11
Android編程實(shí)現(xiàn)任務(wù)管理器的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)任務(wù)管理器的方法,涉及Android針對程序與進(jìn)程操作的相關(guān)技巧,需要的朋友可以參考下2015-12-12
zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的實(shí)戰(zhàn)教程
二維碼的應(yīng)用已經(jīng)可以說是非常廣泛了,下面這篇文章主要給大家介紹了關(guān)于zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Win10下Android App安裝配置開發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了Win10下Android App安裝配置開發(fā)環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Android使用 Retrofit 2.X 上傳多文件和多表單示例
本篇文章主要介紹了Android使用 Retrofit 2.X 上傳多文件和多表單示例,具有一定的參考價值,有興趣的小伙伴一起來了解一下2017-08-08

