溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android TabHost實現(xiàn)頂部選項卡的方法

發(fā)布時間:2020-10-30 16:49:33 來源:億速云 閱讀:142 作者:Leah 欄目:開發(fā)技術

今天就跟大家聊聊有關Android TabHost實現(xiàn)頂部選項卡的方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

用TabHost 來實現(xiàn)頂部選項卡,上代碼:activity_main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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);
        }
      }
    });
  }
}

看完上述內(nèi)容,你們對Android TabHost實現(xiàn)頂部選項卡的方法有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI