BottomNavigationView
在android.support:design:25.0.0
推出缰揪,想學(xué)習(xí)使用,就需要將依賴的design
包升級(jí)到25.0.0
版本,好像兼容到系統(tǒng)版本14
,不是很確定烛缔。個(gè)人感覺(jué)現(xiàn)在兼容到19
應(yīng)該就可以了吧
1. 簡(jiǎn)單使用
先看效果:
優(yōu)點(diǎn):使用很方便馏段;過(guò)度動(dòng)畫(huà)效果不錯(cuò);不用改變顏色來(lái)標(biāo)記狀態(tài)践瓷,也能有直觀的選中狀態(tài)
槽點(diǎn):由于有動(dòng)畫(huà)效果院喜,總覺(jué)得比例有點(diǎn)別扭,尤其是默認(rèn)情況下晕翠,選中第一個(gè)或者最后一個(gè)tab
時(shí)
不過(guò)感覺(jué)優(yōu)點(diǎn)完全可以蓋住槽點(diǎn)喷舀,以后打算使用
素材圖片是通過(guò)一個(gè)Android Stuido
插件android material design icon generator
,進(jìn)行下載的淋肾。這個(gè)插件中有官方全套的Material Design
圖片素材硫麻,安裝這個(gè)插件后,可以通過(guò)new
最后一個(gè)選項(xiàng)Material Design Icon
進(jìn)行下載想要素材樊卓,也可以在布局文件窗口直接快捷鍵ctrl + alt +m
進(jìn)入下載界面
1.1 布局文件 <p>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView
android:id="@+id/bnv_bottom_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorAccent"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/bottom_view" />
</RelativeLayout>
關(guān)鍵在于四個(gè)見(jiàn)名知意的app
屬性
創(chuàng)建一個(gè)menu
文件夾拿愧,新建名字為bottom_view.xml
的Menu resource
文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:icon="@drawable/ic_account_balance_white_24dp"
android:title="首頁(yè)" />
<item
android:icon="@drawable/ic_face_white_24dp"
android:title="好友" />
<item
android:icon="@drawable/ic_accessibility_white_24dp"
android:title="廣場(chǎng)" />
<item
android:icon="@drawable/ic_settings_white_24dp"
android:title="設(shè)置" />
</menu>
目前為止,對(duì)Menu
的基礎(chǔ)知識(shí)碌尔,了解也不多浇辜,感覺(jué)也得學(xué)習(xí)學(xué)習(xí)啦
2. 在Activity使用 <p>
public class BottomViewActivity extends AppCompatActivity {
private MenuItem lastItem; // 上一個(gè)選中的item
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_view);
initView();
}
/**
* 初始化
*/
private void initView() {
BottomNavigationView bnv = (BottomNavigationView) findViewById(R.id.bnv_bottom_activity);
//拿到默認(rèn)選中的item
lastItem = bnv.getMenu().getItem(0);
//點(diǎn)擊選擇item
bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (lastItem != item) { // 判斷當(dāng)前點(diǎn)擊是否為item自身
lastItem = item;
String title = item.getTitle().toString();
Toast.makeText(BottomViewActivity.this, title, Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
}
稍微做了一點(diǎn)的處理,點(diǎn)擊當(dāng)前選中的tab
無(wú)效
記得之前學(xué)習(xí)使用RudioGroup
來(lái)實(shí)現(xiàn)底部的tab
導(dǎo)航時(shí)唾戚,各種選中未選中狀態(tài)奢赂,比較繁瑣,BottomNavigionView
的確很方便颈走。簡(jiǎn)單掃了一眼源碼,代碼量相當(dāng)?shù)纳僭凼浚槿鸽m小五臟俱全立由,涉及的知識(shí)也是不少的,以后再進(jìn)行學(xué)習(xí)序厉,嘗試定制化
3. 最后 <p>
之前很少關(guān)注官方新推出的各種庫(kù)锐膜,完全不知道都有哪些好用的控件和工具,真是錯(cuò)過(guò)一大堆好的學(xué)習(xí)資源弛房,罪過(guò)罪過(guò)道盏。以后要開(kāi)始留意跟進(jìn)學(xué)習(xí)
本人很菜,有錯(cuò)誤請(qǐng)指出
共勉 :)