最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 使用FragmentTabhost代替Tabhost

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

Android 使用FragmentTabhost代替Tabhost

前言:

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

一:首先我們看看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 就貼一個(gè)Fragment XML吧!其他的幾個(gè)都一樣,只是顏色不一樣,呵呵!

<?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[] = { "首頁(yè)", "消息", "好友", "搜索", "更多" }; 
 /** 
  * 
  * 
  */ 
 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的個(gè)數(shù) 
  int count = mFragmentArray.length; 
  for (int i = 0; i < count; i++) { 
   // 給每個(gè)Tab按鈕設(shè)置圖標(biāo)、文字和內(nèi)容 
   TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]) 
     .setIndicator(getTabItemView(i)); 
   // 將Tab按鈕添加進(jìn)Tab選項(xiàng)卡中 
   mTabHost.addTab(tabSpec, mFragmentArray[i], null); 
   // 設(shè)置Tab按鈕的背景 
   mTabHost.getTabWidget().getChildAt(i) 
     .setBackgroundResource(R.drawable.selector_tab_background); 
  } 
 } 
 
 /** 
  * 
  * 給每個(gè)Tab按鈕設(shè)置圖標(biāo)和文字 
  */ 
 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其他幾個(gè)都一樣,指不過(guò)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

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

相關(guān)文章

  • Android畫中畫窗口開啟方法

    Android畫中畫窗口開啟方法

    Android8.0 Oreo(API Level26)允許活動(dòng)啟動(dòng)畫中畫Picture-in-picture(PIP)模式。PIP是一種特殊類型的多窗口模式,主要用于視頻播放。PIP模式已經(jīng)可用于Android TV,而Android8.0則讓該功能可進(jìn)一步用于其他Android設(shè)備
    2023-01-01
  • asynctask的用法詳解

    asynctask的用法詳解

    Android UI操作并不是線程安全的并且這些操作必須在UI線程中執(zhí)行,本文將為您介紹asynctask的用法
    2012-11-11
  • Android自制九宮格解鎖控件

    Android自制九宮格解鎖控件

    這篇文章主要為大家詳細(xì)介紹了Android自制九宮格解鎖控件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android ListView萬(wàn)能適配器實(shí)例代碼

    Android ListView萬(wàn)能適配器實(shí)例代碼

    本文主要介紹Android ListView萬(wàn)能適配器,這里整理了詳細(xì)的資料及實(shí)現(xiàn)代碼,以及實(shí)現(xiàn)效果圖,有需要的小伙伴可以參考下
    2016-09-09
  • RxJava之網(wǎng)絡(luò)請(qǐng)求最常見的三種場(chǎng)景

    RxJava之網(wǎng)絡(luò)請(qǐng)求最常見的三種場(chǎng)景

    本文想闡述一下當(dāng)你開發(fā)Android應(yīng)用并采用RxJava作為你的架構(gòu),尤其是有關(guān)網(wǎng)絡(luò)請(qǐng)求時(shí)最常見的三種場(chǎng)景。這篇文章主要介紹了RxJava之網(wǎng)絡(luò)請(qǐng)求最常見的三種場(chǎng)景,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android編程中自定義dialog用法實(shí)例

    Android編程中自定義dialog用法實(shí)例

    這篇文章主要介紹了Android編程中自定義dialog用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android自定義dialog的定義.布局及調(diào)用等相關(guān)技巧,需要的朋友可以參考下
    2015-12-12
  • Android橫向進(jìn)度條演示效果

    Android橫向進(jìn)度條演示效果

    這篇文章主要為大家詳細(xì)介紹了Android橫向進(jìn)度條的演示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android仿今日頭條APP實(shí)現(xiàn)下拉導(dǎo)航選擇菜單效果

    Android仿今日頭條APP實(shí)現(xiàn)下拉導(dǎo)航選擇菜單效果

    這篇文章主要為大家詳細(xì)介紹了Android仿今日頭條APP實(shí)現(xiàn)下拉導(dǎo)航選擇菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • WebView 實(shí)現(xiàn)全屏播放視頻的示例代碼

    WebView 實(shí)現(xiàn)全屏播放視頻的示例代碼

    這篇文章主要介紹了WebView 實(shí)現(xiàn)全屏播放視頻的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • 優(yōu)化和瘦身Android APK的六個(gè)小技巧

    優(yōu)化和瘦身Android APK的六個(gè)小技巧

    Android應(yīng)用的大小對(duì)用戶體驗(yàn)和應(yīng)用性能至關(guān)重要,大型APK文件會(huì)增加應(yīng)用的安裝時(shí)間,啟動(dòng)時(shí)間和頁(yè)面加載時(shí)間,降低了用戶體驗(yàn),因此,APK瘦身是Android開發(fā)中的重要任務(wù),在本文中,我們將分享6個(gè)小技巧,幫助你優(yōu)化和瘦身Android應(yīng)用,需要的朋友可以參考下
    2023-11-11

最新評(píng)論

马公市| 平泉县| 罗田县| 汾阳市| 伊川县| 山东省| 元谋县| 观塘区| 桂东县| 眉山市| 美姑县| 若尔盖县| 云和县| 凯里市| 谢通门县| 嘉黎县| 张家口市| 宣化县| 碌曲县| 榕江县| 马山县| 庆云县| 青岛市| 麦盖提县| 乐山市| 响水县| 日喀则市| 沙雅县| 洪泽县| 新密市| 衡东县| 拉孜县| 新疆| 石台县| 淮安市| 夏邑县| 辉县市| 姚安县| 阿鲁科尔沁旗| 张家界市| 小金县|