Android FragmentTabHost底部選項卡實現

來源:互聯網
上載者:User

標籤:more   都對   vertica   encoding   inf   androi   item   運行   cal   

記錄一下底部選項卡的實現,很常見的代碼,大神勿嘲笑。

說一下思路,在activity底部要放上FragmentTabHost放上選項,幾個無所謂,每個選項卡都對應一個fragment,點擊選項卡顏色改變可以用selector(選取器)來實現,焦點選中的時候一個顏色,失去焦點的時候,另一個顏色。

首先,activity布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.sdtz.wenmingweifang.MainActivity">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:id="@+id/fl">    </FrameLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:background="#ffffff"        android:gravity="center"        android:orientation="vertical">        <android.support.v4.app.FragmentTabHost            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@+id/fl"            android:id="@+id/tabHost">        </android.support.v4.app.FragmentTabHost>    </LinearLayout></RelativeLayout>



acitivity中主要代碼的代碼,其中view1,view2.....就是選項卡的內容,一般就是一張圖片,下面一段文字(首頁啥的),標紅的部分,就是每個介面的fragment,可以在各個fragment中寫每個介面的代碼

 tabHost = (FragmentTabHost) findViewById(R.id.tabHost);        view = LayoutInflater.from(getBaseContext()).inflate(R.layout.pop_window1,null);        view1 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_view,null);        view2 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_2_view,null);        view3 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_3,null);        view4 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_4,null);        view5 = LayoutInflater.from(getBaseContext()).inflate(R.layout.shouye_5,null);        try{            tabHost.setup(this,getSupportFragmentManager(),R.id.fl);            Fragment mainFragment = new MianFragment();            TabHost.TabSpec tabSpec0 =                    tabHost.newTabSpec(str[0]).setIndicator(view1);            tabHost.addTab(tabSpec0,mainFragment.getClass(),null);            Fragment SecondFragment = new SencondFragment();            TabHost.TabSpec tabSpec2 =                    tabHost.newTabSpec(str[1]).setIndicator(view2);            tabHost.addTab(tabSpec2,SecondFragment.getClass(),null);            Fragment ShezhiFragmet = new ShezhiFragment();            TabHost.TabSpec tabSpec1 =                    tabHost.newTabSpec(str[2]).setIndicator(view3);            tabHost.addTab(tabSpec1,ShezhiFragmet.getClass(),null);            Fragment MoreFragment = new MoreFragment();            TabHost.TabSpec tabSpec4 =                    tabHost.newTabSpec(str[3]).setIndicator(view4);            tabHost.addTab(tabSpec4,MoreFragment.getClass(),null);            Fragment MeFragment = new MeFragment();            TabHost.TabSpec tabSpec5 =                    tabHost.newTabSpec(str[4]).setIndicator(view5);            tabHost.addTab(tabSpec5,MeFragment.getClass(),null);        }catch (Exception e){            e.printStackTrace();        }

下面是選項卡的xml,標誌的部分就是 選取器,標記的部分就是選取器,用來選項卡的顏色變化

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:layout_marginTop="@dimen/y10"        android:layout_width="match_parent"        android:layout_height="20dp"        android:src="@drawable/shouye_view_select"        android:id="@+id/img"/>    <TextView        android:layout_marginBottom="@dimen/y10"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center"        android:textColor="@drawable/shouye_1_text"        android:textSize="12sp"        android:text="首頁"/></LinearLayout>

選取器代碼:

就是有兩張不同顏色的圖片,焦點選中和不選中切換不同的圖片,android:state_selected="true",就是焦點選中,

文字的選取器也是一樣,切換不同的顏色

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:state_selected="true"        android:drawable="@drawable/home_on">    </item>    <item android:drawable="@drawable/home">    </item></selector>

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:state_selected="true"        android:color="#fd0101">    </item>    <item  android:color="#000000">    </item></selector>

至此,選項卡的代碼已經基本完成,調試一下就可以運行了

Android 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.