【Android UI設計與開發】第08期:底部功能表列(三)Fragment+FragmentTabHost實現仿新浪微博底部功能表列

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   os   使用   

轉載請註明出處:http://blog.csdn.net/yangyu20121224/article/details/9016223      

    

      在上一篇文章中,我們花了大量的篇幅來講解Fragment這個新引進類的使用,目的就是為了讓大家能夠牢牢的掌握它的使用方法,以便讀者在今後的開發中能夠熟練的使用它。

 

 

一、實現

 

 

 

二、項目工程結構

 

三、詳細代碼編寫

 

1、主tab布局介面,main_tab_layout:

<?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/maintab_toolbar_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按鈕選項布局,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、fragment布局介面,這裡只列出一個,fragment_1.xml:

<span style="font-size:12px;"><?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" >    <ImageView        android:id="@+id/imageview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:scaleType="fitCenter"        android:src="@drawable/xianjian01" >    </ImageView></LinearLayout></span>

4、Tab選項的自訂按鈕資源檔,列出其中一個按鈕,tab_home_btn:

<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"/>    <item android:drawable="@drawable/icon_home_nor"/></selector></span>

5、Tab選項按鈕背景資源檔,selector_tab_background.xml:

<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"/>    <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"/></selector></span>

6、主Activity類,MainTabActivity.java:

<span style="font-size:12px;">package com.yangyu.mycustomtab02;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;/** * @author yangyu *    功能描述:自訂TabHost */public class MainTabActivity extends FragmentActivity{        //定義FragmentTabHost對象    private FragmentTabHost mTabHost;        //定義一個布局    private LayoutInflater layoutInflater;            //定義數組來存放Fragment介面    private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};        //定義數組來存放按鈕圖片    private int mImageViewArray[] = {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};        //Tab選項卡的文字    private String mTextviewArray[] = {"首頁", "訊息", "好友", "廣場", "更多"};        public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_tab_layout);                initView();    }         /**     * 初始化組件     */    private void initView(){        //執行個體化布局對象        layoutInflater = LayoutInflater.from(this);                        //執行個體化TabHost對象,得到TabHost        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);                    //得到fragment的個數        int count = fragmentArray.length;                            for(int i = 0; i < count; i++){                //為每一個Tab按鈕設定表徵圖、文字和內容            TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));            //將Tab按鈕添加進Tab選項卡中            mTabHost.addTab(tabSpec, fragmentArray[i], null);            //設定Tab按鈕的背景            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);        }    }                    /**     * 給Tab按鈕設定表徵圖和文字     */    private View getTabItemView(int index){        View view = layoutInflater.inflate(R.layout.tab_item_view, null);            ImageView imageView = (ImageView) view.findViewById(R.id.imageview);        imageView.setImageResource(mImageViewArray[index]);                TextView textView = (TextView) view.findViewById(R.id.textview);                textView.setText(mTextviewArray[index]);            return view;    }}</span>

7、Fragment頁面,FragmentPage1.java:

<span style="font-size:12px;">package com.yangyu.mycustomtab02;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentPage1 extends Fragment{    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {                return inflater.inflate(R.layout.fragment_1, null);            }    }</span>

 

【Android UI設計與開發】第08期:底部功能表列(三)Fragment+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.