4.底部導航欄BottomNavigationBar
實現底部導航欄的方式是多種多樣的,今天學習一下谷歌剛出的底部導航欄控制元件。
一、在使用BottomNavigation前需要自己先匯入依賴並手動新增xml資源以及java類
庫檔案可以從gitHub地址:https://github.com/Ashok-Varma/BottomNavigation下載
實現效果如下:
在gitHub中檔案大概是這樣的
1.我們需要用到的只有bottom-navigation-bar,在這個資料夾下找到名叫ashokvarma的資料夾並將其複製到com資料夾下,如下圖所示:
2.將res資料夾下的資原始檔加入已經建立的專案中(有重名的就只加入其中的資源),若不加入這些資原始檔那麼在執行時會出現找不到R檔案的情況,因為ashokvarma中的類會用到這些xml檔案中的變數或者其他資源。
3.在github中找到如下位置,點選下載JAR檔案
4.完成後點選File->project structure選擇Dependencies選項卡,點選右邊的加號選擇jar dependency,然後選擇剛剛下載的jar檔案並確認,最後進行sync。
二、完成以上步驟後我們就已經成功添加了依賴(他喵的是真的費勁,真不知道那些直接寫一句compile“xxxx.xxxx”的是怎們新增的,反正我這樣新增有若干錯誤,望哪位大神指導一下如何像他們一樣簡單的新增依賴)
準備工作已經完成了,接下來需要編輯佈局檔案以及java程式了
1.佈局檔案(其實只需要把navigationBar新增到xml檔案中就行)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<TextView
android:layout_width= "wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id ="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
></com.ashokvarma.bottomnavigation.BottomNavigationBar>
</RelativeLayout>
2.java程式
(1)若只是在xml檔案中添加了導航欄就執行,那麼導航欄中不會出現任東西,因此我們還需要往其中新增專案。需要在oncreate()中新增以下程式段
BottomNavigationBar iconBar;
TextBadgeItem badgeItem = new TextBadgeItem();
badgeItem.setHideOnSelect(false).setText("10").setBackgroundColorResource(R.color.coloryello).setBorderWidth(0);//設定圖示右上角的圓圈加數字的顯示屬性
//筆記:除了這個TextBadgeItem之外還有一個ShapeBadgeItem(右上角變成五角形或者其他的)
iconBar =(BottomNavigationBar)findViewById(R.id.bottom_navigation);
iconBar.setMode(BottomNavigationBar.MODE_SHIFTING);
//筆記:底部導航欄有三種MODE
1)Default 預設效果 選中會突出,有水紋背景的變化 item未選中時不顯示文字
2)FIXED 選中圖形不會突出 item未選中時顯示文字
3)SHIFTING 與Default效果近似
iconBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
//筆記:1)STATIC 點選時沒有水波紋效果
2)RIPPLE 點選時有水波紋效果
3)DEFAULT mode為fixed則使用static mode使用shifting則使用RIPPLE
這兩個設定需要放在前面才有效
iconBar.addItem(new BottomNavigationItem(R.drawable.jiapurson,R.string.firsticonname).setActiveColorResource(R.color.colorgreen).setBadgeItem(badgeItem))
.addItem(new BottomNavigationItem(R.drawable.information
,R.string.secondiconname).setActiveColorResource(R.color.coloryello))
.addItem(new BottomNavigationItem(R.drawable.icon_three,R.string.thirdiconname).setActiveColorResource(R.color.colorPrimaryDark))
.addItem(new BottomNavigationItem(R.drawable.icon_four,R.string.forthiconname).setActiveColorResource(R.color.HONG));//嚮導航欄中新增item setBadgeItem()用於設定右上角的訊息提醒
iconBar.setFirstSelectedPosition(0).initialise();//設定初始選中位置
(2)在新增完導航欄後我們需要對導航欄的item新增監聽,以完成item間的切換動作。每一個切換項都是一個Fregment,每一個Fragment都可以像activity一樣載入XML佈局檔案完成頁面佈局,所以我們需要在此建立4個佈局(fragment1…)和一個Fragment的子類。每個activity中可以包含多個Fragment,兩者具有相同的生命週期。
接下來看具體的程式碼
Fragment010.java
package com.example.r.achat;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment010 extends Fragment {
private int layout=R.layout.fragment1;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
LayoutInflater lf = LayoutInflater.from(getContext());
//筆記:getContext() 這是在View類中提供的方法,用於獲取當前View執行的Context。
View view = lf.inflate(layout,container,false);//載入佈局並將其轉化為VIEW物件。
return view;
}
public void setLayout(int layout)//將layout下的佈局檔名傳入
{
this.layout = layout;
}
}
fragment1.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:id = "@+id/fragment_layout1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="first fragment"
/>
</LinearLayout>
然後我們來看看監聽程式,這段程式需要寫在oncreate()中
iconBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
@Override
public void onTabSelected(int position) {
switch(position)
{
case 0 :
FragmentManager fm = getSupportFragmentManager();//建立一個Fragment管理器
FragmentTransaction ft = fm.beginTransaction();//開啟事務
Fragment010 fs = new Fragment010();
fs.setLayout(R.layout.fragment1);//建立一個fragment物件並將佈局名作為引數傳入其中
ft.replace(R.id.activity_main,fs);//佈局替換,這裡需要注意第一個引數是activity佈局
ft.commit();//提交事務
break;
case 1:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager.beginTransaction();
Fragment010 fragment010 = new Fragment010();
fragment010.setLayout(R.layout.fragment2);
fragmentTransaction1.replace(R.id.activity_main,fragment010);
fragmentTransaction1.commit();
break;
case 2:
FragmentManager fragmentManager1= getSupportFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager1.beginTransaction();
Fragment010 fragment0101 = new Fragment010();
fragment0101.setLayout(R.layout.fragment3);
fragmentTransaction2.replace(R.id.activity_main,fragment0101);
fragmentTransaction2.commit();
break;
case 3:
FragmentManager fragmentManager2= getSupportFragmentManager();
FragmentTransaction fragmentTransaction3 = fragmentManager2.beginTransaction();
Fragment010 fragment0102 = new Fragment010();
fragment0102.setLayout(R.layout.fragment4);
fragmentTransaction3.replace(R.id.activity_main,fragment0102);
fragmentTransaction3.commit();
break;
default:
Toast.makeText(MainActivity.this,"default",Toast.LENGTH_SHORT).show();
FragmentManager fm1 = getSupportFragmentManager();
FragmentTransaction ft1 = fm1.beginTransaction();
Fragment010 fs1 = new Fragment010();
fs1.setLayout(R.layout.fragment1);
ft1.replace(R.id.activity_main,fs1);
ft1.commit();
}