Android Design Support Library——Navigation View

来源:http://www.cnblogs.com/shiwei-bai/archive/2016/01/18/5138305.html
-Advertisement-
Play Games

前沿 Android 從5.0開始引入了Material design元素的設計,這種新的設計語言讓整個安卓的用戶體驗煥然一新,google在Android Design Support Library中封裝了一些重要的material design控制項,在這之前其實github上也已經出現了許多....


前沿

  Android 從5.0開始引入了Material design元素的設計,這種新的設計語言讓整個安卓的用戶體驗煥然一新,google在Android Design Support Library中封裝了一些重要的material design控制項,在這之前其實github上也已經出現了許多各種各樣的material design 控制項,只不過現在google把有些控制項標準化了,註意這個Android Design Support Library和Android Support Library是有區別的,Android Support Library 只是支持了一些基本控制項的封裝,而Android Design Support Library主要是一些Material design特效的實現,Android Design Support Library包括以下八種控制項:

  Navigation View——抽屜導航

  TextInputLayout——EditText懸浮標簽

  Floating Action Button——懸浮操作按鈕

  Snackbar——提示(類似Toast)

  TabLayout——選項卡

  CoordinatorLayout——滾動控制

  CollapsingToolbarLayout——可摺疊的Toolbar(Google+、photos中的效果)

  AppBarLayout——容器AppBar

  本文分章對以上控制項主要做使用方式介紹,暫不分析源碼。首先看Navigation View

  Navigation View——抽屜導航

  非常實用的一種抽屜導航效果,支持直接通過菜單資源文件直接生成導航標簽,實現起來也非常簡單,效果如下圖所示:

  

  使用步驟:

  (1)app的build.gradle中引入design包本文design包版本以23.1.1為例

dependencies {
    compile 'com.android.support:design:23.1.1'
}

  (2)xml佈局文件(activity_main.xml)和SlideMenu一樣需要使用到DrawerLayout。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include layout="@layout/include_list_viewpager" /><!-- 展示內容區域的佈局-->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>

  兩個重要的屬性

  app:headerLayout ——導航頭佈局
  app:menu——導航菜單佈局

  layout_gravity——用來控制左滑和右滑的,start左滑,end右滑,其實這個是跟DrawerLayout有關的,具體可參考DrawerLayout的使用。

  nav_header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:background="?attr/colorPrimaryDark"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Username"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

</LinearLayout>

  drawer_view.xml(重點是看如何實現分組的)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single"><!-- 實現單選-->
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_dashboard"
            android:title="Home" />
        <item
            android:id="@+id/nav_messages"
            android:icon="@drawable/ic_event"
            android:title="Messages" />
        <item
            android:id="@+id/nav_friends"
            android:icon="@drawable/ic_headset"
            android:title="Friends" />
        <item
            android:id="@+id/nav_discussion"
            android:icon="@drawable/ic_forum"
            android:title="Discussion" />
    </group>

    <item android:title="Sub items">
        <menu>
            <item
                android:icon="@drawable/ic_dashboard"
                android:title="Sub item 1" />
            <item
                android:icon="@drawable/ic_forum"
                android:title="Sub item 2" />
        </menu>
    </item>

</menu>

  (2)在代碼中聲明使用

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        menuItem.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
        }

  在onNavigationItemSelected()方法中就可以獲取導航菜單中的每個Item進而實現相應的功能了。

  擴展:如果你想讓你的導航菜單在status bar 上也顯示需要進行如下設置主要針對5.0及以上版本

  ../valuse-v21/styles.xml

<resources>

    <style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

</resources>

  同時在DrawerLayout中加入如下屬性

android:fitsSystemWindows="true"

  如果你感覺Navigation View還不夠強大,可以看看MaterialDrawer,連接如下:https://github.com/mikepenz/MaterialDrawer

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 上:↑ 下:↓ 左:← 右:→Ctrl + 1 : 背景變為綠色Ctrl + 2 : 背景變為黃色Ctrl + 3 : 背景變為藍色Ctrl + ↑ : 放大Ctrl + ↓ : 縮小 //被移動的方塊【DIV】
  • calc是單詞calculate的縮寫,是Css3的一個新的長度單位函數,可以使用簡單的數學運算.Firefox要使用-moz-calc()私有屬性,Chrome要使用-webkit-calc()私有屬性,IE9原生支持標準的不帶首碼的寫法,Opera暫還不支持.運算規則可以使用 “+” “-” “...
  • 1 2 3 4 5 男 6 女 7 中國 8 美國 9 13 14 15
  • //格式化functiontimeCon2(obj){ if(obj==""||obj==undefined){ returnfalse; }else{ vartimeHtml=""; vartime=obj; time=time.split(''); varyear=time[2]//年...
  • 效果圖:查看效果:http://hovertree.com/jq/hovertreeimg/下載:http://hovertree.com/h/bjaf/gk8mko69.htm使用代碼: HovertreeImg Home - 何問起 --> ...
  • 前沿 上一篇介紹了NavigationView的主要使用方式,本章主要介紹TextInputLayout的使用方式。 TextInputLayout——EditText懸浮標簽 TextInputLayout主要作為EditText的父容器來使用,不能單獨使用。TextInputLayout解...
  • 【版權所有,轉載請註明出處。】項目關鍵java代碼為,將tv設置為從jni讀取的字元串,這裡的破解內容是直接從apk動態調試修改最終顯示在tv里的字元內容1.工具介紹Apktool,反編譯源碼用Androidstudio+smalidea插件,動態調試用2.準備工作A.配置插件下載插件smalide...
  • Android設備中大多都內置了感測器,這些感測器有些是基於硬體的,有些是基於軟體的。通過感測器,可以監控設備的三維運動、位置,可以推斷用戶的動作,如傾斜、震動、旋轉等,可以監控設備周圍環境的變化等。 在Android平臺中,我們可以通過SensorManager訪問、監聽感測器,並獲取相應...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...