Android 側(cè)滑菜單

自定義側(cè)滑菜單布局(NavigationView+DrawerLayout)的實現(xiàn)

效果圖

Animation.gif

添加依賴

compile 'com.android.support:design:25.0.1'

簡單布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            android:background="@color/colorAccent"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center_horizontal"
            android:text="你好 側(cè)滑菜單!"
            android:textAllCaps="false"
            android:textColor="@color/colorAccent"
            android:textSize="25sp"/>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="420dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
      ></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

代碼實現(xiàn)

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private DrawerLayout mDrawerLayout;
    private Toolbar mToolbar;
    private NavigationView mNavigationView;
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
        mToolbar = (Toolbar) findViewById(R.id.tool_bar);
        mToolbar.setTitle("littonishir");
        mToolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.white));
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_layout_open, R.string.drawer_layout_close);
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        //自定義NavigationView布局加載
        View headerView = mNavigationView.inflateHeaderView(R.layout.nav_header_main);
        ImageView imageView = (ImageView) headerView.findViewById(R.id.iv_head);
        TextView textView = (TextView)headerView.findViewById(R.id.tv_username);
        LinearLayout ll_1 = (LinearLayout)headerView.findViewById(R.id.ll_one);
        LinearLayout ll_2 = (LinearLayout)headerView.findViewById(R.id.ll_two);
        LinearLayout ll_3 = (LinearLayout)headerView.findViewById(R.id.ll_thr);
        LinearLayout ll_4 = (LinearLayout)headerView.findViewById(R.id.ll_four);
        LinearLayout ll_5 = (LinearLayout)headerView.findViewById(R.id.ll_fi);
        imageView.setOnClickListener(this);
        textView.setOnClickListener(this);
        ll_1.setOnClickListener(this);
        ll_2.setOnClickListener(this);
        ll_3.setOnClickListener(this);
        ll_4.setOnClickListener(this);
        ll_5.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_head :
                Snackbar.make(view, "我是圖片", Snackbar.LENGTH_SHORT).show();
                break;

            case R.id.tv_username:
                Snackbar.make(view, "我是名字", Snackbar.LENGTH_SHORT).show();
                break;
            case R.id.ll_one:
                Snackbar.make(view, "ITEMLALALA1", Snackbar.LENGTH_SHORT).show();
                break;
            case R.id.ll_two:
                Snackbar.make(view, "ITEMLALALA2", Snackbar.LENGTH_SHORT).show();
                break;
            case R.id.ll_thr:
                Snackbar.make(view, "ITEMLALALA3", Snackbar.LENGTH_SHORT).show();
                break;
            case R.id.ll_four:
                Snackbar.make(view, "ITEMLALALA4", Snackbar.LENGTH_SHORT).show();
                break;
            case R.id.ll_fi:
                Snackbar.make(view, "ITEMLALALA5", Snackbar.LENGTH_SHORT).show();
                break;
        }

    }

}

歡迎start 紅心

源碼下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市厨内,隨后出現(xiàn)的幾起案子祈秕,更是在濱河造成了極大的恐慌,老刑警劉巖雏胃,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件请毛,死亡現(xiàn)場離奇詭異,居然都是意外死亡瞭亮,警方通過查閱死者的電腦和手機方仿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來统翩,“玉大人仙蚜,你說我怎么就攤上這事∷艚桑” “怎么了鳍征?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長面徽。 經(jīng)常有香客問我艳丛,道長匣掸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任氮双,我火速辦了婚禮碰酝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘戴差。我一直安慰自己送爸,他們只是感情好,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布暖释。 她就那樣靜靜地躺著袭厂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪球匕。 梳的紋絲不亂的頭發(fā)上纹磺,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天,我揣著相機與錄音亮曹,去河邊找鬼橄杨。 笑死,一個胖子當著我的面吹牛照卦,可吹牛的內(nèi)容都是我干的式矫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼役耕,長吁一口氣:“原來是場噩夢啊……” “哼采转!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蹄葱,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤氏义,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后图云,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惯悠,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年竣况,在試婚紗的時候發(fā)現(xiàn)自己被綠了克婶。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡丹泉,死狀恐怖情萤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情摹恨,我是刑警寧澤筋岛,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站晒哄,受9級特大地震影響睁宰,放射性物質(zhì)發(fā)生泄漏搂妻。R本人自食惡果不足惜爹土,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一洋满、第九天 我趴在偏房一處隱蔽的房頂上張望曼振。 院中可真熱鬧,春花似錦红符、人聲如沸青柄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽致开。三九已至,卻和暖如春萎馅,著一層夾襖步出監(jiān)牢的瞬間喇喉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工校坑, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人千诬。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓耍目,卻偏偏與公主長得像,于是被迫代替她去往敵國和親徐绑。 傳聞我的和親對象是個殘疾皇子邪驮,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容