国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

FragmentTabHost FrameLayout實(shí)現(xiàn)底部導(dǎo)航欄

2019-12-12 03:31:33
字體:
供稿:網(wǎng)友

app經(jīng)常用到底部導(dǎo)航欄,早前使用過RadioGroup+FrameLayout實(shí)現(xiàn)或者RadioGroup+ViewPager實(shí)現(xiàn),現(xiàn)在基本使用FragmentTabHost+FrameLayout來實(shí)現(xiàn),因?yàn)槭褂闷饋砗唵我子谩O旅鎸懸粋€(gè)小例子簡要地總結(jié)一下這個(gè)組合。

首先,看一下例子的最終運(yùn)行效果圖

這里寫圖片描述這里寫圖片描述

這5個(gè)圖標(biāo)的效果其實(shí)都是一樣的,只要做出來一個(gè),以此類推就可以寫出其他幾個(gè)

第一步 FragmentTabHost+FrameLayout布局,先看一下代碼:

<?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"> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/bg_color"/> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost></LinearLayout>

布局大體分為兩部分,上面的FrameLayout代表是顯示內(nèi)容部分,下面的FragmentTabHost代表是導(dǎo)航欄部分。注意: FragmentTabHost的id和其內(nèi)部的FrameLayout的id必須是系統(tǒng)的id。

第二步, FragmentTabHost+FrameLayout代碼實(shí)現(xiàn)連接,F(xiàn)ragmentTabHost使用,可以記住三個(gè)步驟:(1)setup(…)可以理解為,初始化底部導(dǎo)航和內(nèi)容頁面連接,(2)新建TabSpec可以理解為初始化底部菜單項(xiàng),(3)addTab(…)可以理解為把菜單和內(nèi)容添加到控件中。下面看一下代碼:

public class MainActivity extends AppCompatActivity { private LayoutInflater mInflater; private FragmentTabHost mTabHost; private ArrayList<TabDataBean> tabDataList = new ArrayList<>(5); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mInflater = LayoutInflater.from(this); mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); //第一步,初始化fTabHost, 第三個(gè)參數(shù)為內(nèi)容容器 mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); //第二步,初始化菜單項(xiàng) TabHost.TabSpec tabSpec = mTabHost.newTabSpec("主頁"); /*添加菜單項(xiàng)布局*/ View view = mInflater.inflate(R.layout.tab_indicator, null); ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon); TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text); iconTab.setImageResource(R.drawable.tab_home_normal); tvTab.setText("主頁"); tabSpec.setIndicator(view); //第三步,添加菜單項(xiàng)和內(nèi)容 mTabHost.addTab(tabSpec, HomeFragment.class, null);// initTabHost(); }}

其中涉及了菜單項(xiàng)布局tab_indicator.xml,內(nèi)容頁布局HomeFragment.java文件,代碼如下:

tab_indicator.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" android:paddingTop="3dp" android:paddingBottom="3dp" android:layout_gravity="center" android:gravity="center"> <ImageView android:id="@+id/iv_tab_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/tabTextColor" android:layout_marginTop="2dp"/></LinearLayout>

HomeFragment.java

public class HomeFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_home, null); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Toast.makeText(getContext(), R.string.tabHome, Toast.LENGTH_SHORT).show(); }}

fragment_home.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" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/themeColor" android:text="@string/tabHome"/></LinearLayout>

寫完如上代碼的運(yùn)行效果如下:

這里寫圖片描述

可以看到一個(gè)菜單項(xiàng)已經(jīng)顯示出來,照葫蘆畫瓢,我們就可以吧其他四個(gè)菜單項(xiàng)寫出來,既然其他四項(xiàng)和這個(gè)代碼雷同,所以說肯定有公共部分可以抽取出來,減少代碼量和代碼整潔度。我們發(fā)現(xiàn),有三個(gè)變量隨著菜單變化的,如:菜單圖標(biāo),菜單名稱,菜單對(duì)應(yīng)的內(nèi)容。所以我們寫一個(gè)類封裝一下,代碼如下:

TabDataBean.java

public class TabDataBean { private int tabName; private int tabIcon; private Class content; //對(duì)應(yīng)的內(nèi)容類 public TabDataBean(int tabName, int tabIcon, Class content) { this.tabName = tabName; this.tabIcon = tabIcon; this.content = content; } public int getTabName() { return tabName; } public void setTabName(int tabName) { this.tabName = tabName; } public int getTabIcon() { return tabIcon; } public void setTabIcon(int tabIcon) { this.tabIcon = tabIcon; } public Class getContent() { return content; } public void setContent(Class content) { this.content = content; }}

有了這個(gè)實(shí)體類,我們就可以把上面的第一步和第二步驟抽取出來封裝一下了,代碼如下:

private void initTabHost() { //初始化fTabHost, 第三個(gè)參數(shù)為內(nèi)容容器 mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); /*初始化數(shù)據(jù)源*/ TabDataBean bean = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class); //添加底部菜單項(xiàng)-tabSpec TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName())); //給菜單項(xiàng)添加內(nèi)容,indicator,其中indicator需要的參數(shù)View即為菜單項(xiàng)的布局 tabSpec.setIndicator(getIndicatorView(bean)); //第二參數(shù)就是該菜單項(xiàng)對(duì)應(yīng)的頁面內(nèi)容 mTabHost.addTab(tabSpec, bean.getContent(), null)}private View getIndicatorView(TabDataBean bean){ View view = mInflater.inflate(R.layout.tab_indicator, null); ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon); TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text); iconTab.setImageResource(bean.getTabIcon()); tvTab.setText(bean.getTabName()); return view; }

把其他四項(xiàng)添加入后,代碼如下:

public class MainActivity extends AppCompatActivity { private LayoutInflater mInflater; private FragmentTabHost mTabHost; private ArrayList<TabDataBean> tabDataList = new ArrayList<>(5); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mInflater = LayoutInflater.from(this); mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); initTabHost(); } /** * 初始化底部導(dǎo)航欄 */ private void initTabHost() { //初始化fTabHost, 第三個(gè)參數(shù)為內(nèi)容容器 mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); /*初始化數(shù)據(jù)源*/ TabDataBean tabHome = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class); TabDataBean tabHot = new TabDataBean(R.string.tabHot, R.drawable.tab_life_normal, HotFragment.class); TabDataBean tabCategory = new TabDataBean(R.string.tabCategory, R.drawable.tab_service_normal, CategoryFragment.class); TabDataBean tabCart = new TabDataBean(R.string.tabCart, R.drawable.tab_order_normal, CartFragment.class); TabDataBean tabMine = new TabDataBean(R.string.tabMine, R.drawable.tab_mine_normal, MineFragment.class); tabDataList.add(tabHome); tabDataList.add(tabHot); tabDataList.add(tabCategory); tabDataList.add(tabCart); tabDataList.add(tabMine); //添加底部菜單項(xiàng)-tabSpec for (TabDataBean bean : tabDataList) { TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName())); //給菜單項(xiàng)添加內(nèi)容,indicator,其中indicator需要的參數(shù)View即為菜單項(xiàng)的布局 tabSpec.setIndicator(getIndicatorView(bean)); //第二參數(shù)就是該菜單項(xiàng)對(duì)應(yīng)的頁面內(nèi)容 mTabHost.addTab(tabSpec, bean.getContent(), null); } } /** * 初始化indciator的內(nèi)容 * @param bean */ private View getIndicatorView(TabDataBean bean){ View view = mInflater.inflate(R.layout.tab_indicator, null); ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon); TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text); iconTab.setImageResource(bean.getTabIcon()); tvTab.setText(bean.getTabName()); return view; }}

運(yùn)行效果如下:

這里寫圖片描述這里寫圖片描述

如上結(jié)果,已經(jīng)離我們的目標(biāo)很近了。

第三步,給圖標(biāo)和文字添加變色selector
首先,給圖標(biāo)變色,在drawable文件夾下新建selector_tab_home.xml,代碼如下:

<?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/tab_home_selected"/> <item android:state_pressed="true" android:drawable="@drawable/tab_home_selected"/> <item android:drawable="@drawable/tab_home_normal"/></selector>

接下來把

TabDataBean tabHome = new TabDataBean(R.string.tabHome, R.drawable.tab_home_normal, HomeFragment.class); 改為
TabDataBean tabHome = new TabDataBean(R.string.tabHome, R.drawable.selector_tab_home, HomeFragment.class);
以此類推,剩下的四項(xiàng)也是如此處理

然后,菜單名稱變色,如果在res文件夾下沒有color資源文件夾,新建color資源文件夾,然后在color文件夾下新建selector_tab_text.xml文件,代碼如下:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/themeColor" android:state_selected="true"/> <item android:color="@color/themeColor" android:state_active="true"/> <item android:color="@color/tabTextColor" android:state_selected="false"/> <item android:color="@color/tabTextColor" android:state_active="false"/></selector>

接下來把tab_indicator.xml文件中TextView的Android:textColor="@color/tabTextColor" 修改為

android:textColor="@color/selector_tab_text"

最后運(yùn)行一下就和文章開頭的運(yùn)行效果一致了,有疑問或者是文章有不對(duì)的地方歡迎評(píng)論和指正^_^。

問題: 我們?cè)诿總€(gè)fragment的onActivityCreated(…)方法中都寫了

Toast.makeText(getContext(), R.string.tabHome, Toast.LENGTH_SHORT).show();

運(yùn)行程序,你會(huì)發(fā)現(xiàn),無論是第一次點(diǎn)擊還是再次進(jìn)入此菜單項(xiàng)時(shí),都會(huì)彈出toast對(duì)話框。如果我們?cè)诿總€(gè)頁面中都寫入了網(wǎng)絡(luò)請(qǐng)求,相當(dāng)于每次進(jìn)入都會(huì)進(jìn)行一次請(qǐng)求。但是項(xiàng)目需求只要求我們第一進(jìn)入該頁面時(shí)請(qǐng)求,所以我們應(yīng)該如何處理呢?有幾種處理方式,大家可以思考一下,下一篇文章,我們重寫FragmentTabHost來處理這個(gè)問題。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安化县| 闵行区| 闸北区| 仁布县| 灵宝市| 公安县| 三门县| 靖宇县| 海原县| 固始县| 务川| 贺州市| 天等县| 临安市| 昌邑市| 禹城市| 松溪县| 濉溪县| 浦北县| 大厂| 拜城县| 元朗区| 克拉玛依市| 中方县| 含山县| 嵩明县| 泊头市| 嘉祥县| 云林县| 兴化市| 巴彦淖尔市| 英吉沙县| 温州市| 新乐市| 龙游县| 滦平县| 诏安县| 商丘市| 松滋市| 贵德县| 金山区|