怎么在android中利用FragmentTabhost實現(xiàn)導(dǎo)航分頁-創(chuàng)新互聯(lián)

這篇文章給大家介紹怎么在android中利用FragmentTabhost實現(xiàn)導(dǎo)航分頁,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、杜集ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的杜集網(wǎng)站制作公司

基本模板

public class MainActivity extends FragmentActivity {
  
  private FragmentTabHost mTabHost;
  private LayoutInflater mLayoutInflater;

  private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
      Fragment3.class, Fragment4.class, Fragment5.class };

  private int mImageArray[] = { R.drawable.tab_home_btn,
      R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
      R.drawable.tab_square_btn, R.drawable.tab_more_btn };

  private String mTextArray[] = { "首頁", "消息", "好友", "搜索", "更多" };

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
  }

  private void initView() {

    mLayoutInflater = LayoutInflater.from(this);

    // 找到TabHost
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
     mTabHost.getTabWidget().setDividerDrawable(null);//去除分割線
    // 得到fragment的個數(shù)
    for (int i = 0; i < mFragmentArray.length; i++) {
      // 給每個Tab按鈕設(shè)置圖標(biāo)、文字和內(nèi)容
      TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
          .setIndicator(getTabItemView(i));
      // 將Tab按鈕添加進(jìn)Tab選項卡中
      mTabHost.addTab(tabSpec, mFragmentArray[i], null);
      // 設(shè)置Tab按鈕的背景
      mTabHost.getTabWidget().getChildAt(i)
          .setBackgroundResource(R.drawable.selector_tab_background);
    }
  }

  //給每個Tab按鈕設(shè)置圖標(biāo)和文字
  private View getTabItemView(int index) {
    View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
    ImageView imageView = view.findViewById(R.id.imageview);
    //設(shè)置圖片選擇器,選中的tab改變圖標(biāo)
    switch (index){
      case 0:imageView.setImageResource(R.drawable.main_bottom_image_selector);break;
      case 1:imageView.setImageResource(R.drawable.main_bottom_image_selector2);break;
      case 2:imageView.setImageResource(R.drawable.main_bottom_image_selector3);break;
      case 3:imageView.setImageResource(R.drawable.main_bottom_image_selector4);break;
      case 4:imageView.setImageResource(R.drawable.main_bottom_image_selector5);break;
    }
    TextView textView = view.findViewById(R.id.textview);
    textView.setText(mTextArray[index]);
    //設(shè)置文本選擇器,選中的tab文字高亮
    textView.setTextColor(R.drawable.main_bottom_text_selector);
    return view;
  }

}

acitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <FrameLayout
  android:id="@+id/realtabcontent"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1" />

 <android.support.v4.app.FragmentTabHost
  android:id="@android:id/tabhost" //必須使用提供的id
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:background="@drawable/bg_tabhost_bg">

  <FrameLayout
   android:id="@android:id/tabcontent" //必須使用提供的id
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:layout_weight="0" />      
 </android.support.v4.app.FragmentTabHost>

</LinearLayout>

tab_item_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:orientation="vertical">
  <ImageView
    android:id="@+id/imageview"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_margin="3dp"
    />
  <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:textSize="12sp"
    android:layout_marginBottom="2dp"/>
</LinearLayout>

main_bottom_image_selector圖片選擇器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="false" android:drawable="@drawable/ic_launcher" />
  <item android:state_selected="true" android:drawable="@drawable/ic_launcher_round" />
</selector>
Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

關(guān)于怎么在android中利用FragmentTabhost實現(xiàn)導(dǎo)航分頁就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享文章:怎么在android中利用FragmentTabhost實現(xiàn)導(dǎo)航分頁-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://bm7419.com/article20/dichjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、搜索引擎優(yōu)化移動網(wǎng)站建設(shè)、網(wǎng)站排名、企業(yè)建站、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)