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

Android 仿網易新聞主介面:DrawerLayout 側邊欄與 ActionBar 完整配置

編輯:關於 Android 程式設計

新聞類 App 的主介面幾乎都長得差不多:頂部一條帶 logo 與選單的 ActionBar,左側可滑出的抽屜放功能入口。要做出這個效果,需要四塊拼圖——DrawerLayout 側邊欄、ActionBar 的樣式與 logo、menu 選單資源,以及負責雙向動畫的 ActionBarDrawerToggle。動工前請記得先把 theme 換成帶 ActionBar 的主題,例如 android:theme="@android:style/Theme.Holo.Light"。

一、側邊欄:DrawerLayout

依官方文件,DrawerLayout 的第一個子視圖是 Activity 的主內容,第二個才是側邊欄。因此主布局用 FrameLayout 承載內容,再用 include 引入左側抽屜,並以 layout_gravity="start" 指定它從左側出現。

<android.support.v4.widget.DrawerLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/mdrawlaout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffeeeeee">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="這是主界面"/>
    </FrameLayout>

    <include
        android:layout_width="180dp"
        android:layout_height="match_parent"
        layout="@layout/drawer_layout_left"
        android:layout_gravity="start"
        android:clickable="true" />
</android.support.v4.widget.DrawerLayout>

抽屜內容用 CircleImageView 這個開源元件做圓形頭像,屬性設定很直覺,再往下排四個文字選單即可。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <!-- 圓形頭像 -->
    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="https://schemas.android.com/apk/res-auto"
        android:id="@+id/circleimageview"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/circlel_header"
        app:border_color="#ff0000"
        app:border_width="2dp" />

    <!-- 選單列表 -->
    <TextView
        android:id="@+id/tv_item1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:gravity="center"
        android:text="夜間工具"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_item2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="繪畫工具"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_item3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="測試1"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_item4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="測試2"
        android:textSize="16sp" />
</LinearLayout>

二、ActionBar 配置

ActionBar 的細項多半得靠程式碼設定:隱藏標題、啟用 logo、換上 logo 圖與背景,並把 App 圖示轉為可點擊的返回鈕。

// 取得 ActionBar
ActionBar actionBar = getActionBar();
// 不顯示標題文字
actionBar.setDisplayShowTitleEnabled(false);
// 啟用 logo 顯示
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setLogo(R.drawable.netease_top);
// 設定背景
Drawable background = getResources().getDrawable(R.drawable.top_bar_background);
actionBar.setBackgroundDrawable(background);
// 將 App 圖示轉為可點擊的返回按鈕
actionBar.setDisplayHomeAsUpEnabled(true);

背景色則交給 XML 定義,用 shape 包一層 solid 即可日後換色。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="@color/top_title_bar_normal_backgrond_color"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="gray">#bbbbbb</color>
    <color name="black">#000000</color>
    <color name="top_title_bar_normal_backgrond_color">#EB413D</color>
    <color name="top_title_bar_button_press_background_color">#D83C38</color>
</resources>

如此一來 ActionBar 就會呈現網易紅。

三、menu 選單資源

修改 menu_main.xml 即可,Android Studio 產生的 onCreateOptionsMenu() 會自動解析它。這裡的技巧是把「更多」做成一個可展開的子選單,讓 overflow 裡也能有圖示與文字。

<menu xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/night_biz_pc_menu_icon"
        android:orderInCategory="1"
        android:title="@string/app_name"
        android:showAsAction="always"/>

    <item
        android:id="@+id/action_btn01"
        android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
        android:orderInCategory="2"
        android:title="更多"
        android:showAsAction="always">
        <menu>
            <item android:id="@+id/action_btn02" android:icon="@drawable/biz_plugin_manage_weather"
                android:orderInCategory="100" android:title="11/13" android:showAsAction="never"/>
            <item android:id="@+id/action_btn03" android:icon="@drawable/biz_plugin_manage_offline"
                android:orderInCategory="100" android:title="離線" android:showAsAction="never"/>
            <item android:id="@+id/action_btn04" android:icon="@drawable/biz_plugin_manage_theme"
                android:orderInCategory="100" android:title="夜間" android:showAsAction="never"/>
            <item android:id="@+id/action_btn05" android:icon="@drawable/biz_plugin_manage_search"
                android:orderInCategory="100" android:title="搜索" android:showAsAction="never"/>
            <item android:id="@+id/action_btn06" android:icon="@drawable/biz_plugin_manage_qrcode"
                android:orderInCategory="100" android:title="掃一掃" android:showAsAction="never"/>
            <item android:id="@+id/action_btn07" android:icon="@drawable/biz_plugin_manage_offline"
                android:orderInCategory="100" android:title="設置" android:showAsAction="never"/>
        </menu>
    </item>
</menu>

四、ActionBarDrawerToggle:讓漢堡圖示與抽屜同步

ActionBarDrawerToggle 是一個開關,用來開啟或關閉抽屜。它把 DrawerLayout 與 ActionBar 綁在一起,點擊 ActionBar 上的 Home 按鈕就能彈出抽屜,滑動抽屜時左上角還會跟著變形。使用時要在兩個回呼中轉發事件:

  • onConfigurationChanged:處理螢幕旋轉等配置變更
  • onOptionsItemSelected:攔截 Home 按鈕的點擊

另外必須在 onPostCreate() 中呼叫 syncState(),讓 toggle 的狀態與抽屜同步,並把漢堡圖示設為 ActionBar 的 Home 圖示。

// 設定 DrawerLayout 的點擊事件
mdrawlayout.setDrawerListener(new MyAPPdrawerlistener());

// 建立抽屜開關
mActionBarDrawerToggle = new ActionBarDrawerToggle(
        this, this.mdrawlayout, R.string.drawer_open, R.string.drawer_close);

/**
 * 該方法會自動與 ActionBar 關聯,把開關圖示顯示在 ActionBar 上。
 * 若不設定,抽屜功能仍可用,只是會使用預設圖示。
 */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mActionBarDrawerToggle.syncState();
}

// 設備配置改變時(例如旋轉螢幕)
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    // 交給 toggle 判斷是否為 Home 按鈕點擊
    return mActionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}

最後在 DrawerLayout 的監聽器中把四個回呼轉發給 toggle,抽屜滑動時圖示就會跟著旋轉。

private class MyAPPdrawerlistener implements DrawerLayout.DrawerListener {

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        mActionBarDrawerToggle.onDrawerSlide(drawerView, slideOffset);
    }

    @Override
    public void onDrawerOpened(View drawerView) {
        mActionBarDrawerToggle.onDrawerOpened(drawerView);
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        mActionBarDrawerToggle.onDrawerClosed(drawerView);
    }

    @Override
    public void onDrawerStateChanged(int newState) {
        mActionBarDrawerToggle.onDrawerStateChanged(newState);
    }
}

附錄:幾個容易混淆的觀念

1. android:paddingLeft 與 android:layout_marginLeft:前者是「內容距容器左邊的距離」,後者是「整個 View 距父容器左邊的距離」。
2. android:gravity 決定控制項「內部內容」的對齊方式,android:layout_gravity 決定控制項「自身」在父容器中的位置。
3. android:orderInCategory 是 ActionBar 中每個 item 的排序權重,數值越大越靠後,位置不足時會被擠進 overflow。
4. 在 Android Studio 中引入 GitHub 上的函式庫,直接把原始碼複製進專案,再在 gradle 中加上對方提供的依賴敘述即可。

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