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

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

Android BottomNavigationView底部導(dǎo)航效果

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

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

接下來我們來看看如何使用BottomNavigationView。

使用BottomNavigationView 需要添加design兼容包的依賴。

dependencies {  //...  compile 'com.android.support:design:25.1.0'}

在 res/menu/ 目錄下創(chuàng)建一個(gè) xml 文件(沒有該目錄則手動(dòng)創(chuàng)建一個(gè)),我將其命名為 navigation.xml,里面使用的圖片資源都是系統(tǒng)自帶的。這個(gè)文件是用來定義導(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)聽事件    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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 镶黄旗| 金昌市| 云南省| 彩票| 丽江市| 大余县| 和田县| 辽阳市| 铅山县| 伊宁县| 玉林市| 策勒县| 馆陶县| 交口县| 漳浦县| 锡林浩特市| 彭水| 吴江市| 卓尼县| 随州市| 新丰县| 准格尔旗| 浦县| 湖州市| 延安市| 东城区| 子长县| 关岭| 乌兰察布市| 那曲县| 锦屏县| 清远市| 平和县| 五寨县| 柯坪县| 诏安县| 吴江市| 南陵县| 永定县| 南城县| 安化县|