欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 使用FragmentTabhost代替Tabhost

 更新時間:2017年05月03日 11:14:01   投稿:lqh  
這篇文章主要介紹了Android 使用FragmentTabhost代替Tabhost的相關(guān)資料,需要的朋友可以參考下

Android 使用FragmentTabhost代替Tabhost

前言:

現(xiàn)在Fragment使用越來越廣了,雖然Fragment寄生在Activity下,但是它的出現(xiàn)對于開發(fā)者來說是一件非常幸運的事,使開發(fā)的效率更高效了,好了下面就說說 FragmentTabhost的使用,因為Tabhost已經(jīng)不推薦使用了,現(xiàn)在一般都使用FragmentTabhost!我本身也個菜鳥,就是幫幫新手,因為Fragment是3.0才出現(xiàn),為了避免3.0以下的使用不了,所以我們要用v4包來支持,不要倒錯包哦!大神勿噴!

一:首先我們看看XML:

1.activity_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" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:background="@drawable/bg_tabhost_bg"> 
 
  <FrameLayout 
   android:id="@android:id/tabcontent" 
   android:layout_width="0dp" 
   android:layout_height="0dp" 
   android:layout_weight="0" />    
 </android.support.v4.app.FragmentTabHost> 
 
</LinearLayout> 

2.tab_item_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:gravity="center" 
 android:orientation="vertical" > 
 
 <ImageView 
  android:id="@+id/imageview" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:focusable="false" 
  android:padding="3dp" 
  android:src="@drawable/tab_home_btn"> 
 </ImageView> 
 
 <TextView 
  android:id="@+id/textview"   
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="" 
  android:textSize="10sp" 
  android:textColor="#ffffff"> 
 </TextView> 
 
</LinearLayout> 

3.fragment1.xml 就貼一個Fragment XML吧!其他的幾個都一樣,只是顏色不一樣,呵呵!

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

ok,XML先寫完了,那我們看看代碼吧!

4.MainActivity

package com.example.fragmenttabhost; 
 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentTabHost; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TabHost.TabSpec; 
import android.widget.TextView; 
 
import com.example.fragment.Fragment1; 
import com.example.fragment.Fragment2; 
import com.example.fragment.Fragment3; 
import com.example.fragment.Fragment4; 
import com.example.fragment.Fragment5; 
 
/** 
 * 
 * @author zqy 
 * 
 */ 
public class MainActivity extends FragmentActivity { 
 /** 
  * FragmentTabhost 
  */ 
 private FragmentTabHost mTabHost; 
 
 /** 
  * 布局填充器 
  * 
  */ 
 private LayoutInflater mLayoutInflater; 
 
 /** 
  * Fragment數(shù)組界面 
  * 
  */ 
 private Class mFragmentArray[] = { Fragment1.class, Fragment2.class, 
   Fragment3.class, Fragment4.class, Fragment5.class }; 
 /** 
  * 存放圖片數(shù)組 
  * 
  */ 
 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); 
  // 得到fragment的個數(shù) 
  int count = mFragmentArray.length; 
  for (int i = 0; i < count; i++) { 
   // 給每個Tab按鈕設(shè)置圖標、文字和內(nèi)容 
   TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]) 
     .setIndicator(getTabItemView(i)); 
   // 將Tab按鈕添加進Tab選項卡中 
   mTabHost.addTab(tabSpec, mFragmentArray[i], null); 
   // 設(shè)置Tab按鈕的背景 
   mTabHost.getTabWidget().getChildAt(i) 
     .setBackgroundResource(R.drawable.selector_tab_background); 
  } 
 } 
 
 /** 
  * 
  * 給每個Tab按鈕設(shè)置圖標和文字 
  */ 
 private View getTabItemView(int index) { 
  View view = mLayoutInflater.inflate(R.layout.tab_item_view, null); 
  ImageView imageView = (ImageView) view.findViewById(R.id.imageview); 
  imageView.setImageResource(mImageArray[index]); 
  TextView textView = (TextView) view.findViewById(R.id.textview); 
  textView.setText(mTextArray[index]); 
 
  return view; 
 } 
 
} 

5.Fragment1.java  Fragment其他幾個都一樣,指不過XML不一樣!

package com.example.fragment; 
 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
import com.example.fragmenttabhost.R; 
 
public class Fragment1 extends Fragment{ 
 
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) { 
   
  return inflater.inflate(R.layout.fragment1, null);  
 }  
} 

OK 基本上寫完了,讓我們看看效果!

哈哈,效果還算可以!好了,去吃飯了!

資源下載地址:http://xiazai.jb51.net/201705/yuanma/FragmentTabhost(jb51.net).rar

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Android Studio下添加assets目錄的實現(xiàn)方法

    Android Studio下添加assets目錄的實現(xiàn)方法

    下面小編就為大家?guī)硪黄狝ndroid Studio下添加assets目錄的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • android6.0權(quán)限動態(tài)申請框架permissiondispatcher的方法

    android6.0權(quán)限動態(tài)申請框架permissiondispatcher的方法

    下面小編就為大家分享一篇android6.0權(quán)限動態(tài)申請框架permissiondispatcher的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android實現(xiàn)閃屏頁效果

    Android實現(xiàn)閃屏頁效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)閃屏頁效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Kotlin by關(guān)鍵字作用及使用介紹

    Kotlin by關(guān)鍵字作用及使用介紹

    Kotlin 中的 by 關(guān)鍵字在 Java 中是沒有的,這使我對它感到非常陌生。Kotlin 中為什么要新增 by 關(guān)鍵字呢?by 關(guān)鍵字在 Kotlin 中是如何使用的?本文會介紹 by 關(guān)鍵字的使用分類,具體的示例,Kotlin 內(nèi)置的 by 使用,希望能夠幫助到大家
    2022-10-10
  • Android AsyncTask的優(yōu)缺點詳解

    Android AsyncTask的優(yōu)缺點詳解

    本文主要介紹了Android AsyncTask的優(yōu)缺點,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • 淺談Android實踐之ScrollView中滑動沖突處理解決方案

    淺談Android實踐之ScrollView中滑動沖突處理解決方案

    涉及到了ViewPager,MapView,ListView,就需要ScrollView來做一下支援,這篇文章主要介紹了淺談Android實踐之ScrollView中滑動沖突處理解決方案,有需要的可以來了解一下。
    2016-12-12
  • android 使用瀏覽器打開指定頁面的實現(xiàn)方法

    android 使用瀏覽器打開指定頁面的實現(xiàn)方法

    這篇文章主要介紹了android 使用瀏覽器打開指定頁面的實現(xiàn)方法,本文通過實例文字說明的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • Android圖像處理之泛洪填充算法

    Android圖像處理之泛洪填充算法

    這篇文章主要介紹了泛洪填充算法,工作原理是從一個點開始附近像素點,填充成新的顏色,直到封閉區(qū)域內(nèi)的所有像素點都被填充新顏色為止,分享給大家供大家參考
    2018-05-05
  • Android編程開發(fā)從零開始編寫一個輕量級瀏覽器

    Android編程開發(fā)從零開始編寫一個輕量級瀏覽器

    這篇文章主要為大家介紹了Android編程開發(fā)從零開始編寫一個輕量級瀏覽器過程步驟示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進步
    2022-02-02
  • android RecycleView實現(xiàn)下拉刷新和上拉加載

    android RecycleView實現(xiàn)下拉刷新和上拉加載

    這篇文章主要為大家詳細介紹了android RecycleView實現(xiàn)下拉刷新和上拉加載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06

最新評論