bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Android中的Tabhost怎么利用FragmentTabhost進行替代

這期內容當中小編將會給大家帶來有關Android中的Tabhost怎么利用FragmentTabhost進行替代,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

為東川等地區用戶提供了全套網頁設計制作服務,及東川網站建設行業解決方案。主營業務為成都做網站、網站建設、外貿營銷網站建設、東川網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業、用心的態度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

Android 使用FragmentTabhost代替Tabhost

前言:

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

一:首先我們看看XML:

1.activity_main.xml

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

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<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吧!其他的幾個都一樣,只是顏色不一樣,呵呵!

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<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數組界面 
  * 
  */ 
 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); 
  // 得到fragment的個數 
  int count = mFragmentArray.length; 
  for (int i = 0; i < count; i++) { 
   // 給每個Tab按鈕設置圖標、文字和內容 
   TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]) 
     .setIndicator(getTabItemView(i)); 
   // 將Tab按鈕添加進Tab選項卡中 
   mTabHost.addTab(tabSpec, mFragmentArray[i], null); 
   // 設置Tab按鈕的背景 
   mTabHost.getTabWidget().getChildAt(i) 
     .setBackgroundResource(R.drawable.selector_tab_background); 
  } 
 } 
 
 /** 
  * 
  * 給每個Tab按鈕設置圖標和文字 
  */ 
 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);  
 }  
} 

上述就是小編為大家分享的Android中的Tabhost怎么利用FragmentTabhost進行替代了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創新互聯行業資訊頻道。

當前題目:Android中的Tabhost怎么利用FragmentTabhost進行替代
文章鏈接:http://vcdvsql.cn/article4/jhioie.html

成都網站建設公司_創新互聯,為您提供動態網站面包屑導航全網營銷推廣服務器托管網站改版網站維護

廣告

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

營銷型網站建設