日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android之Tab类总结

發布時間:2024/7/23 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android之Tab类总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要包括以下Tab類實現方式

  • FragmentTabHost+Fragment實現
  • 傳統的ViewPager實現
  • FragmentManager+Fragment實現
  • ViewPager+FragmentPagerAdapter實現
  • TabPageIndicator+ViewPager+FragmentPagerAdapter
  • FragmentTabHost+Fragment實現

    布局文件

    <?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.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical" ><FrameLayout android:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" ></FrameLayout><TabWidget android:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="60dip" /></LinearLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>

    MainActivity

    package com.darna.wmxfx;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.RelativeLayout; import android.widget.TabHost.OnTabChangeListener; import android.widget.TextView;import com.darna.wmxfx.fragment.Frg_Cart; import com.darna.wmxfx.fragment.Frg_Index; import com.darna.wmxfx.fragment.Frg_UserCenter;public class MainActivity extends FragmentActivity {private FragmentTabHost mTabHost = null;private int iTabIndex = 0;private int[][] iTab = new int[][] { { R.drawable.indexunused, R.drawable.cartunused, R.drawable.usercenterunused },{ R.drawable.indexused, R.drawable.cartused, R.drawable.usercenterused } };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();}private void initViews(){String[] strTab = getResources().getStringArray(R.array.bottom_tab);RelativeLayout[] relativeLayout = new RelativeLayout[strTab.length];for(int i = 0; i < strTab.length; i++){if (i == 0) {relativeLayout[i] = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_bottom, null);relativeLayout[i].findViewById(R.id.iv_bottom_tab).setBackgroundResource(iTab[1][i]);TextView tView = (TextView) relativeLayout[i].findViewById(R.id.tv_bottom_tab);tView.setText(strTab[i]);tView.setTextColor(getResources().getColor(R.color.textused));}else {relativeLayout[i] = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_bottom, null);relativeLayout[i].findViewById(R.id.iv_bottom_tab).setBackgroundResource(iTab[0][i]);TextView t = (TextView) relativeLayout[i].findViewById(R.id.tv_bottom_tab);t.setText(strTab[i]);t.setTextColor(getResources().getColor(R.color.textunused));}}mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_INDEX) .setIndicator(relativeLayout[0]), Frg_Index.class, null); mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_CART) .setIndicator(relativeLayout[1]), Frg_Cart.class, null);mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_USERCENTER) .setIndicator(relativeLayout[2]), Frg_UserCenter.class, null);mTabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {// TODO Auto-generated method stubView v = mTabHost.getTabWidget().getChildAt(iTabIndex);((ImageView) v.findViewById(R.id.iv_bottom_tab)).setBackgroundResource(iTab[0][iTabIndex]);((TextView) v.findViewById(R.id.tv_bottom_tab)).setTextColor(getResources().getColor(R.color.textunused));v = mTabHost.getCurrentTabView();iTabIndex = mTabHost.getCurrentTab();((ImageView) v.findViewById(R.id.iv_bottom_tab)).setBackgroundResource(iTab[1][iTabIndex]);((TextView) v.findViewById(R.id.tv_bottom_tab)).setTextColor(getResources().getColor(R.color.textused));}});} }

    效果如下

    傳統的ViewPager實現

    FragmentManager+Fragment實現

    ViewPager+Fragment實現

    TabPageIndicator+ViewPager+FragmentPagerAdapter

    實現方式和3是一致的,但是使用了TabPageIndicator作為tab的指示器,效果還是不錯的,這個之前寫過,就不再貼代碼了。
    效果圖:

    后四種方式詳細可以參見

    Android項目Tab類型主界面大總結 Fragment+TabPageIndicator+ViewPager - Hongyang - 博客頻道 - CSDN.NET

    完成

    總結

    以上是生活随笔為你收集整理的Android之Tab类总结的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。