Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Material Design 實戰:NavigationView 與 Palette 取色器

編輯:關於 Android 開發

DrawerLayout + NavigationView 的組合用法

NavigationView 是 Material Design 中實現側滑導航菜單的標準控件。通常我們會把它和 DrawerLayout 搭配使用:DrawerLayout 裡放兩個子控件,程序主界面放在 NavigationView 前面。

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

    <include layout="@layout/app_main" android:layout_width="match_parent" android:layout_height="match_parent" />

    <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_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

這裡有兩個重要屬性:headerLayout 和 menu,分別表示 header 部分佈局和菜單資源。

程序主界面 app_main 包含 AppBarLayout、content_main 以及一個 FloatingActionButton:

<android.support.design.widget.CoordinatorLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

注意事項:DrawerLayout 指定 openDrawer 為 start,NavigationView 指定 layout_gravity 為 start;並為 NavigationView 設定 headerLayout 和 menu 屬性。

headerLayout 佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">
    <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing" android:src="@android:drawable/sym_def_app_icon" />
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
    <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="android.studio@android.com" />
</LinearLayout>

調用 setNavigationItemSelectedListener() 後,菜單項被選中時會透過 OnNavigationItemSelectedListener 得到回調,你可以在其中判斷是哪個菜單項被點擊,進而加載新內容、更新選中狀態或關閉 drawer。menu 菜單資源如下:

<menu xmlns:android="https://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:id="@+id/nav_camera" android:icon="@drawable/ic_menu_camera" android:title="Import" />
        <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="Gallery" />
        <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow" />
        <item android:id="@+id/nav_manage" android:icon="@drawable/ic_menu_manage" android:title="Tools" />
    </group>
    <item android:title="Communicate">
        <menu>
            <item android:id="@+id/nav_share" android:icon="@drawable/ic_menu_share" android:title="Share" />
            <item android:id="@+id/nav_send" android:icon="@drawable/ic_menu_send" android:title="Send" />
        </menu>
    </item>
</menu>

Activity 中通過 ActionBarDrawerToggle 關聯 Drawer 與 Toolbar,並在 onNavigationItemSelected 中處理菜單點擊:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Toolbar 設置時要注意:setTitle 的文字需在 setSupportActionBar 之前調用,否則會無效;菜單監聽則可在 Toolbar 上設置,也可像 ActionBar 那樣通過回調方法處理。

設置 ActionBar 及 StatusBar 背景顏色(API 21 及以上):

if (android.os.Build.VERSION.SDK_INT >= 21) {
    Window window = getWindow();
    window.setStatusBarColor(colorBurn(vibrant.getRgb()));
    window.setNavigationBarColor(colorBurn(vibrant.getRgb()));
}

其中 colorBurn 用來把顏色加深約 10%,讓狀態欄、游標與底部導航欄的色調更統一:

private int colorBurn(int RGBValues) {
    int red = RGBValues >> 16 & 0xFF;
    int green = RGBValues >> 8 & 0xFF;
    int blue = RGBValues & 0xFF;
    red = (int) Math.floor(red * (1 - 0.1));
    green = (int) Math.floor(green * (1 - 0.1));
    blue = (int) Math.floor(blue * (1 - 0.1));
    return Color.rgb(red, green, blue);
}

Palette 取色器

根據圖片來決定標題的顏色和標題欄的背景色,能讓視覺上更具衝擊力和新鮮感,而不像統一色調那樣呆板。Palette 能讓你從圖像中提取突出的顏色,包括 Vibrant(充滿活力)、Vibrant dark、Vibrant light、Muted(柔和)、Muted dark、Muted light 這六種。

使用時,在加載圖片的後台線程中把位圖對象傳給 Palette.generate() 靜態方法;若不使用線程,則調用 Palette.generateAsync() 並提供監聽器:

Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
    @Override public void onGenerated(Palette palette) {
        Palette.Swatch vibrant = palette.getVibrantSwatch();
        if (vibrant != null) {
            // 更新標題 TextView 的背景色與文字色
            titleView.setBackgroundColor(vibrant.getRgb());
            titleView.setTextColor(vibrant.getTitleTextColor());
        }
    }
});

上面的代碼展示了如何從圖片提取顏色,把 TextView 的背景設為圖片主色調,再用 getTitleTextColor() 設置一個匹配的文字顏色。若 TextView 有背景圖片、想讓字體與之協調,用 getBodyTextColor() 更合適。

size 的問題:size 越大耗時越長,越小可選色彩越少。最佳選擇要根據圖片用途:頭像之類建議 24-32;風景大圖建議 8-16;默認為 16。

創建 Palette 實例有四種方法(同步的 generate 與異步的 generateAsync,各自可指定是否傳入 size)。創建完成後,可用以下方法取得六種 swatch:

Vibrant.       Palette.getVibrantSwatch()
Vibrant dark.  Palette.getDarkVibrantSwatch()
Vibrant light. Palette.getLightVibrantSwatch()
Muted.         Palette.getMutedSwatch()
Muted dark.    Palette.getDarkMutedSwatch()
Muted light.   Palette.getLightMutedSwatch()

大多數情況下我們都使用 Vibrant 和 Dark Vibrant。每個 swatch 都提供 getPopulation()(佔比像素數)、getRgb()(RGB 值)、getHsl()(HSL 值)、getBodyTextColor() 與 getTitleTextColor() 等方法,方便你靈活搭配配色。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved