Android BottomNavigationView底部導(dǎo)航效果
BottomNavigationView 很早之前就在 Material Design 中出現(xiàn)了,但是直到 Android Support Library 25 中才增加了 BottomNavigationView 控件。也就是說(shuō)如果使用官方的BottomNavigationView控件必須讓targetSdkVersion >= 25,這樣才能引入25版本以上的兼容包。

接下來(lái)我們來(lái)看看如何使用BottomNavigationView。
使用BottomNavigationView 需要添加design兼容包的依賴(lài)。
dependencies {
//...
compile 'com.android.support:design:25.1.0'
}
在 res/menu/ 目錄下創(chuàng)建一個(gè) xml 文件(沒(méi)有該目錄則手動(dòng)創(chuàng)建一個(gè)),我將其命名為 navigation.xml,里面使用的圖片資源都是系統(tǒng)自帶的。這個(gè)文件是用來(lái)定義導(dǎo)航條目具體的信息。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/call"
android:icon="@android:drawable/ic_menu_call"
android:title="call" />
<item
android:id="@+id/message"
android:icon="@android:drawable/ic_dialog_email"
android:title="message" />
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_menu_search"
android:title="搜索" />
<item
android:id="@+id/delete"
android:icon="@android:drawable/ic_menu_delete"
android:title="刪除"/>
</menu>
每個(gè)item表示底部導(dǎo)航的一個(gè)條目,icon是圖標(biāo),title是文字。
然后修改Activity布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="演示內(nèi)容"
android:textSize="36sp"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@android:color/black"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation"/>
</RelativeLayout>
BottomNavigationView有幾個(gè)特殊的屬性,
* itemtBackground 條目背景
* itemIcoTint 圖標(biāo)渲染的顏色
* itemtTextColor 文字的顏色
* menu 關(guān)聯(lián)上面創(chuàng)建的菜單
最后修改BottomNavigationViewActivity代碼
public class BottomNavigationViewActivity extends AppCompatActivity {
private TextView textView;
private BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation_view);
textView = (TextView) findViewById(R.id.text);
navigationView = (BottomNavigationView) findViewById(R.id.navigation);
//選中條目的監(jiān)聽(tīng)事件
navigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
textView.setText(item.getTitle().toString());
return true;
}
});
}
}
注意事項(xiàng)
* 底部導(dǎo)航欄默認(rèn)高度是56dp
* 菜單建議是3-5個(gè)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android記事本項(xiàng)目開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了Android記事本項(xiàng)目開(kāi)發(fā)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android查看電池電量的方法(基于BroadcastReceiver)
這篇文章主要介紹了Android查看電池電量的方法,結(jié)合實(shí)例分析了Android使用BroadcastReceiver實(shí)現(xiàn)針對(duì)電池電量的查詢(xún)技巧,需要的朋友可以參考下2016-01-01
Flutter Recovering Stream Errors小技巧
這篇文章主要為大家介紹了Flutter Recovering Stream Errors小技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
android自定義RadioGroup可以添加多種布局的實(shí)現(xiàn)方法
這篇文章介紹了android自定義RadioGroup可以添加多種布局的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-08-08
Android程序啟動(dòng)時(shí)出現(xiàn)黑屏問(wèn)題的解決方法
這篇文章主要介紹了Android程序啟動(dòng)時(shí)出現(xiàn)黑屏問(wèn)題的解決方法,分析了黑屏出現(xiàn)的原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-08-08
Android Studio一直處于Building的兩種解決方法
很多朋友都遇到過(guò)打開(kāi)別人的項(xiàng)目一直處于Building‘XXX’Gradle project info的情況。下面小編給大家?guī)?lái)了Android Studio一直處于Building的解決方法,感興趣的朋友一起看看吧2018-08-08
Android實(shí)現(xiàn)彈窗進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)彈窗進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

