Android中怎么實現(xiàn)底部狀態(tài)欄切換,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
專注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)富蘊免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
1、實現(xiàn)步驟:
(1)、布局文件中定義TabLayout控件 (2)、定義切換的每個Fragment布局文件 (3)、定義切換的每個Fragment的Java類 (4)、定義TabLayoutMainActivity類 (5)、效果圖演示
2、實現(xiàn)過程:
(1)、布局文件中定義TabLayout控件(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.showly.bottomnavigationbardemo.TabLayoutMainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager_content_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="none" /> <android.support.design.widget.TabLayout android:id="@+id/tab_layout_view" android:layout_width="match_parent" android:layout_height="50dp" app:tabGravity="fill" app:tabIndicatorHeight="0dp" app:tabMode="fixed" app:tabSelectedTextColor="#FB8081" app:tabTextColor="#A0A0A0" /></LinearLayout>
(2)、定義切換的每個Fragment布局文件(fragment_frist.xml)
這里有四個Tab類別(首頁、娛樂、游戲、我的),布局都類似,這里只貼出其中一個
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="首頁" android:textColor="#000"/></RelativeLayout>
(3)、定義切換的每個Fragment的Java類(FristFragment.class)
這里的Java 類實現(xiàn)方式也相似,貼出其中一個
package com.showly.bottomnavigationbardemo.fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.showly.bottomnavigationbardemo.R;public class FristFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_frist, null); return view; }}
(4)、定義TabLayoutMainActivity類(TabLayoutMainActivity.class)
package com.showly.bottomnavigationbardemo;import android.os.Bundle;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import com.showly.bottomnavigationbardemo.fragment.FourthlyFragment;import com.showly.bottomnavigationbardemo.fragment.FristFragment;import com.showly.bottomnavigationbardemo.fragment.SecondFragment;import com.showly.bottomnavigationbardemo.fragment.ThirtlyFragment;public class TabLayoutMainActivity extends AppCompatActivity { //未選中的Tab圖片 private int[] unSelectTabRes = new int[]{R.drawable.i8live_menu_home_normal , R.drawable.i8live_menu_information_normal, R.drawable.i8live_menu_game_normal, R.drawable.i8live_menu_personl_normal}; //選中的Tab圖片 private int[] selectTabRes = new int[]{R.drawable.i8live_menu_home_press, R.drawable.i8live_menu_information_press , R.drawable.i8live_menu_game_press, R.drawable.i8live_menu_personl_press}; //Tab標(biāo)題 private String[] title = new String[]{"首頁", "娛樂", "游戲", "我的"}; private ViewPager viewPager; private TabLayout tabLayout; private TabLayout.Tab tabAtOne; private TabLayout.Tab tabAttwo; private TabLayout.Tab tabAtthree; private TabLayout.Tab tabAtfour; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide();//隱藏掉整個ActionBar setContentView(R.layout.activity_main); initView(); initData(); initListener(); } private void initView() { viewPager = (ViewPager) findViewById(R.id.viewpager_content_view); tabLayout = (TabLayout) findViewById(R.id.tab_layout_view); //使用適配器將ViewPager與Fragment綁定在一起 viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager())); //將TabLayout與ViewPager綁定 tabLayout.setupWithViewPager(viewPager); /* //設(shè)置方式一: //獲取底部的單個Tab tabAtOne = tabLayout.getTabAt(0); tabAttwo = tabLayout.getTabAt(1); tabAtthree = tabLayout.getTabAt(2); tabAtfour = tabLayout.getTabAt(3); //設(shè)置Tab圖片 tabAtOne.setIcon(R.drawable.i8live_menu_home_press); tabAttwo.setIcon(R.drawable.i8live_menu_information_normal); tabAtthree.setIcon(R.drawable.i8live_menu_game_normal); tabAtfour.setIcon(R.drawable.i8live_menu_personl_normal);*/ //設(shè)置方式二: for (int i = 0; i < title.length; i++) { if (i == 0) { tabLayout.getTabAt(0).setIcon(selectTabRes[0]); } else { tabLayout.getTabAt(i).setIcon(unSelectTabRes[i]); } } } private void initData() { } private void initListener() { //TabLayout切換時導(dǎo)航欄圖片處理 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) {//選中圖片操作 for (int i = 0; i < title.length; i++) { if (tab == tabLayout.getTabAt(i)) { tabLayout.getTabAt(i).setIcon(selectTabRes[i]); viewPager.setCurrentItem(i); } } } @Override public void onTabUnselected(TabLayout.Tab tab) {//未選中圖片操作 for (int i = 0; i < title.length; i++) { if (tab == tabLayout.getTabAt(i)) { tabLayout.getTabAt(i).setIcon(unSelectTabRes[i]); } } } @Override public void onTabReselected(TabLayout.Tab tab) { } }); } //自定義適配器 public class MyFragmentPagerAdapter extends FragmentPagerAdapter { public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if (position == 1) { return new SecondFragment();//娛樂 } else if (position == 2) { return new ThirtlyFragment();//游戲 } else if (position == 3) { return new FourthlyFragment();//我的 } return new FristFragment();//首頁 } @Override public int getCount() { return title.length; } @Override public CharSequence getPageTitle(int position) { return title[position]; } }}
(5)、效果圖演示
1、實現(xiàn)步驟:
(1)、布局文件中定義FragmentTabHost控件 (2)、定義底部菜單欄布局 (3)、定義切換的每個Fragment布局文件 (4)、定義切換的每個Fragment的Java類 (5)、切換按鈕的圖片 (6)、定義FragmentTabHostMainActivity類 (7)、效果圖演示
2、實現(xiàn)過程:
(1)、布局文件中定義FragmentTabHost控件(fragment_tabhost_activity.xml)
<?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:background="#fff" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/vp_pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="none" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:background="#3000" android:layout_height="65dp"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v4.app.FragmentTabHost></LinearLayout>
(2)、定義底部菜單欄布局(tab_content.xml)
<?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:padding="2dp" android:orientation="vertical"> <ImageView android:id="@+id/iv_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:background="@drawable/i8live_menu_home_normal" /> <TextView android:id="@+id/tv_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:text="首頁" /></LinearLayout>
(3)、定義切換的每個Fragment布局文件(fragment_frist.xml)
這里有四個Tab類別(首頁、娛樂、游戲、我的),布局都類似,這里只貼出其中一個
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="首頁" android:textColor="#000"/></RelativeLayout>
(4)、定義切換的每個Fragment的Java類(FristFragment.class)
這里的Java 類實現(xiàn)方式也相似,貼出其中一個
package com.showly.bottomnavigationbardemo.fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.showly.bottomnavigationbardemo.R;public class FristFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_frist, null); return view; }}
(5)、切換按鈕的圖片(tab_main.xml)
這里有四個是相似的,只貼出其中一個
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 切換選中之后的圖片--> <item android:drawable="@drawable/i8live_menu_home_press" android:state_selected="true"/> <!-- 未選中的圖片--> <item android:drawable="@drawable/i8live_menu_home_normal"/></selector>
(6)、定義FragmentTabHostMainActivity類(FragmentTabHostMainActivity.class)
package com.showly.bottomnavigationbardemo;import android.graphics.Color;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.app.FragmentTabHost;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TabHost;import android.widget.TabWidget;import android.widget.TextView;import com.showly.bottomnavigationbardemo.fragment.FourthlyFragment;import com.showly.bottomnavigationbardemo.fragment.FristFragment;import com.showly.bottomnavigationbardemo.fragment.SecondFragment;import com.showly.bottomnavigationbardemo.fragment.ThirtlyFragment;import java.util.ArrayList;import java.util.List;public class FragmentTabHostMainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener { private int[] selectTabRes = new int[]{R.drawable.tab_main, R.drawable.tab_infomation , R.drawable.tab_game, R.drawable.tab_personal}; //Tab標(biāo)題 private String[] title = new String[]{"首頁", "娛樂", "游戲", "我的"}; private Class fragmentArry[] = {FristFragment.class, SecondFragment.class, ThirtlyFragment.class, FourthlyFragment.class}; private List<Fragment> fragmentList = new ArrayList(); private ViewPager viewPager; private FragmentTabHost tabHost; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_tabhost_activity); initView(); initData(); initListener(); } /** * 初始化Fragment并給ViewPager添加適配器 */ private void initVaper() { FristFragment fristFragment = new FristFragment(); SecondFragment secondFragment = new SecondFragment(); ThirtlyFragment thirtlyFragment = new ThirtlyFragment(); FourthlyFragment fourthlyFragment = new FourthlyFragment(); fragmentList.add(fristFragment); fragmentList.add(secondFragment); fragmentList.add(thirtlyFragment); fragmentList.add(fourthlyFragment); //ViewPager添加適配器 viewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragmentList)); tabHost.getTabWidget().setDividerDrawable(null); } private void initView() { viewPager = (ViewPager) findViewById(R.id.vp_pager); tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);//綁定tabhost tabHost.setup(this, getSupportFragmentManager(), R.id.vp_pager);//TabHost綁定viewpager //獲取item的個數(shù) int count = title.length; for (int i = 0; i < count; i++) { //設(shè)置每個TabHost布局 TabHost.TabSpec tabSpec = tabHost.newTabSpec(title[i]) .setIndicator(getTabItemView(i)); //item與fragment關(guān)聯(lián) tabHost.addTab(tabSpec, fragmentArry[i], null); tabHost.setTag(i); } //初始化TabHost文字顏色 upDateTab(tabHost); //給ViewPager設(shè)置適配器 initVaper(); } /** * 更新文字顏色。 * * @param mTabHost */ private void upDateTab(FragmentTabHost mTabHost) { for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) { TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.tv_item); if (mTabHost.getCurrentTab() == i) {//選中 tv.setTextColor(Color.parseColor("#FF5959")); } else {//不選中 tv.setTextColor(Color.parseColor("#777777")); } } } /** * 設(shè)置每個Item布局 */ private View getTabItemView(int i) { View view = LayoutInflater.from(this).inflate(R.layout.tab_content, null); ImageView itemImg = (ImageView) view.findViewById(R.id.iv_imageview); TextView itemText = (TextView) view.findViewById(R.id.tv_item); itemImg.setBackgroundResource(selectTabRes[i]); itemText.setText(title[i]); return view; } private void initData() { } private void initListener() { viewPager.addOnPageChangeListener(this); tabHost.setOnTabChangedListener(this); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { TabWidget widget = tabHost.getTabWidget(); int oldFocusability = widget.getDescendantFocusability(); widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);//設(shè)置View覆蓋子類控件而直接獲得焦點 tabHost.setCurrentTab(position);//根據(jù)位置Postion設(shè)置當(dāng)前的Tab widget.setDescendantFocusability(oldFocusability);//設(shè)置取消分割線 } @Override public void onPageScrollStateChanged(int state) { } @Override public void onTabChanged(String tabId) { int position = tabHost.getCurrentTab(); viewPager.setCurrentItem(position);//把選中的Tab的位置賦給適配器,讓它控制頁面切換 upDateTab(tabHost);//設(shè)置TabHost文字顏色 } /** * 適配器 * */ public class MyFragmentAdapter extends FragmentPagerAdapter { List<Fragment> list; public MyFragmentAdapter(FragmentManager fm, List<Fragment> list) { super(fm); this.list = list; } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }
關(guān)于Android中怎么實現(xiàn)底部狀態(tài)欄切換問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
網(wǎng)頁標(biāo)題:Android中怎么實現(xiàn)底部狀態(tài)欄切換
URL鏈接:http://jinyejixie.com/article4/ghhpie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、Google、全網(wǎng)營銷推廣、網(wǎng)站排名、用戶體驗、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)