1. 程式人生 > >DrawerLayout 側拉選單

DrawerLayout 側拉選單

//佈局

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
xmlns:tools=“http://schemas.android.com/tools
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:id="@+id/drawerlayout"
android:orientation=“vertical”
tools:context=".MainActivity">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextColor="@color/colorText"
        app:title="我是toolbar"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="我是內容頁" />
</LinearLayout>

<android.support.design.widget.NavigationView
android:id="@+id/navigtion"
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:layout_gravity=“start”
app:elevation=“5dp”
app:headerLayout="@layout/navigation_header"
app:menu="@menu/main">
</android.support.design.widget.NavigationView>

<!--<LinearLayout
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:layout_gravity="start">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="我是側滑頁"/>
</LinearLayout>-->

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

//menu選單裡的main.xml

<?xml version="1.0" encoding="utf-8"?>
    android:id="@+id/item_One"
    android:title="打卡"></item>
<item
    android:id="@+id/item_Two"
    android:title="點名"></item>
<item
    android:id="@+id/item_Three"
    android:title="睡覺"></item>
<group
    android:id="@+id/group1">
    <item
        android:id="@+id/item_Four"
        android:title="設定"></item>
    <item
        android:id="@+id/item_Five"
        android:title="我的"></item>

</group>

//頭部

<?xml version="1.0" encoding="utf-8"?>


imageView
android:id="@+id/Head_Image"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:src="@mipmap/ic_launcher_round"
android:layout_centerInParent=“true”/>

//mainactivity

package com.bawei.wangyaxiao.d9drawablelayout;
public class MainActivity extends AppCompatActivity {

private NavigationView navigtion;
private DrawerLayout drawerlayout;
private ImageView mImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    DrawerLayout v;

}

private void initView() {

    navigtion = (NavigationView) findViewById(R.id.navigtion);
    drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);

    //從NavigationView裡面獲取頭佈局
    View view = navigtion.getHeaderView(0);
    mImage = view.findViewById(R.id.Head_Image);
    mImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"睡覺",Toast.LENGTH_SHORT).show();
        }
    });

    //這個是選單的點選事件
    navigtion.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.item_One:
                    Toast.makeText(MainActivity.this, "itemOne", Toast.LENGTH_SHORT).show();
                    drawerlayout.closeDrawers();
                    break;
                case R.id.item_Two:
                    Toast.makeText(MainActivity.this, "itemTwo", Toast.LENGTH_SHORT).show();
                    drawerlayout.closeDrawers();
                    break;
                case R.id.item_Three:
                    Toast.makeText(MainActivity.this, "itemThree", Toast.LENGTH_SHORT).show();
                    drawerlayout.closeDrawers();
                    break;

            }
            return false;
        }
    });

}

}