Android新手入門2016(14)--FragmentTabHost實現選項卡和菜單

來源:互聯網
上載者:User

標籤:set   新手   ati   i++   顏色不同   自己   ffffff   屌絲   addtab   

本文來自肥寶傳說之路,引用必須註明出處!

這章憋了好久。本來想寫選項卡的,學到TabHost,TabWidget的,把代碼拿過來準備研究的時候,發現竟然在4.0.3版本號碼被廢棄了。

百度一下,發如今後面的版本號碼,用FragmentTabHost和LayoutInflater來取代了。網上也有一些關於Frame的內容,可是都不是新手教程的。

寫得不夠通俗。想直接拿代碼下來研究,發現竟然非常多人都是上傳程式碼片段,然後再給個收費連結。作為一個窮屌絲,僅僅能自己一點一點去研究了。

Frament是什嗎?直譯是片段的意思,Frament是為瞭解決Android同一套代碼在不同尺寸螢幕的裝置上顯示的問題而誕生的,是Activity的一個部分。

事實上就是把介面分成一部分一部分的方便管理。更深入的瞭解能夠看這裡點擊開啟連結

FragmentActivity 繼承自Activity。可以使用Frament相關內容的Activity。


FragmentTabHost 替代了TabHost的類

來,先看個圖:



呃。。

。MacBook的圖就是打。

。。

邊看代碼邊說吧:

activity_hello_world.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/bottom_bar"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost></LinearLayout>


我們能夠看出,菜單條須要一個布局檔案來控制這些button的擺放。同一時候,索引標籤頁面面內,也是須要有一個布局檔案。


tab_item_view.xml   tabbutton的布局檔案。button由圖片ImageView和文字TextView兩部分組成

<?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/bottom_bar">  </ImageView>  <TextView    android:id="@+id/textview"       android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:text=""    android:textSize="12sp"    android:textColor="#FFFFFF">  </TextView></LinearLayout>


fragment1.xml   頁面fragment的布局檔案 C1FFC1是背景顏色。

<?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="#C1FFC1" ></LinearLayout>

fragment2.xml   這裡僅僅是顏色不同而已

  android:background="#EEEE00"

fragment3.xml   這裡僅僅是顏色不同而已

  android:background="#FFFFFF"

fragment4.xml   這裡僅僅是顏色不同而已

  android:background="#C6555D"

fragment5.xml   這裡僅僅是顏色不同而已

  android:background="#000000"

Fragment1.java 就讀一下布局檔案
package com.fable.helloworld;import com.fable.helloworld.R;import com.fable.helloworld.R.layout;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup; public class Fragment1 extends Fragment{  @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container,      Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment1, null);//fragment2345事實上也就是這裡不同而已。  }}

HelloWorldActivity.java 主要邏輯實現

package com.fable.helloworld; import android.os.Bundle;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.fable.helloworld.Fragment1;import com.fable.helloworld.R;public class HelloWorldActivity extends FragmentActivity {  //FragmentActivity 能夠對Fragment進行操作的Activity  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.home,R.drawable.ic_dialog_email, R.drawable.ic_menu_my_calendar,R.drawable.ic_search_category_default, R.drawable.ic_input_add };   //選項卡文字    private String mTextArray[] = { "首頁", "訊息", "好友", "搜尋", "很多其它" };   public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_hello_world);    initView();//初始化視圖  }  /**   * 初始化組件   */  private void initView() {    mLayoutInflater = LayoutInflater.from(this);//布局填充,動態布局用到        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);// 找到TabHost選項卡    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);//選項卡容器    // 得到fragment的個數    int count = mFragmentArray.length;    for (int i = 0; i < count; i++) {      // 給每一個Tabbutton設定表徵圖、文字和內容      TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));      // 將Tabbutton加入進Tab選項卡中      mTabHost.addTab(tabSpec, mFragmentArray[i], null);//第二個參數就是選項卡相應頁面的詳細內容      // 設定Tabbutton的背景      mTabHost.getTabWidget().getChildAt(i)          .setBackgroundResource(R.drawable.bottom_bar);    }  }  /**   *   * 給每一個Tabbutton設定表徵圖和文字   */  private View getTabItemView(int index) {    View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);//tab的動態布局    ImageView imageView = (ImageView) view.findViewById(R.id.imageview);    imageView.setImageResource(mImageArray[index]);//設定表徵圖    TextView textView = (TextView) view.findViewById(R.id.textview);    textView.setText(mTextArray[index]);//設定文字    //這裡能夠看出,往一個視圖view裡面插東西。就是先通過id找出裡面元素對象,然後在對對象進行操作    return view;  }}

代碼不多。但檔案多了一點,所以上傳了代碼,點擊開啟連結




Android新手入門2016(14)--FragmentTabHost實現選項卡和菜單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.