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: android:layout_width='match_parent' android:layout_height='0dp'> <LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'> <TabWidgetandroid: android:layout_width='match_parent'android:layout_height='wrap_content' /> <FrameLayoutandroid: android:layout_width='match_parent'android:layout_height='match_parent'><LinearLayout android: android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'></LinearLayout><LinearLayout android: android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'></LinearLayout><LinearLayout android: 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)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JSP出現(xiàn)中文亂碼問題解決方法詳解2. PHP?strstr函數(shù)原型源碼分析3. ThinkPHP6使用JWT+中間件實現(xiàn)Token驗證實例詳解4. vue的路由動畫切換頁面無法讀取meta值的bug記錄5. 不使用XMLHttpRequest對象實現(xiàn)Ajax效果的方法小結(jié)6. ASP.NET MVC限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)7. ASP基礎(chǔ)入門第二篇(ASP基礎(chǔ)知識)8. 怎樣打開XML文件?xml文件如何打開?9. ASP.NET MVC實現(xiàn)登錄后跳轉(zhuǎn)到原界面10. TP5使用RabbitMQ實現(xiàn)消息隊列的項目實踐
