1. 程式人生 > >android 抽屜式佈局的使用

android 抽屜式佈局的使用


1.首先定義主佈局
 

<?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:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.dejun.androidcourse.activity.ThirdActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false">

        <com.dejun.commonsdk.wight.CustomSearchView
            android:id="@+id/csv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:search_backgound="#ff00"
            app:search_hint="搜尋內容"
            app:search_left_icon="@drawable/arrow_left_back"
            app:search_right_text="完成"></com.dejun.commonsdk.wight.CustomSearchView>
        <com.dejun.commonsdk.wight.BottomMenuView
            android:id="@+id/bmv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:fifth_drawable="@drawable/tab_better_pressed"
            app:fifth_text="你好"
            app:menu_center_visible="true"
            app:menu_text_color="#000"
            app:menu_text_size="12sp" />
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/csv"
            android:layout_above="@id/bmv">
        </FrameLayout>
    </RelativeLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationview"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"//定義抽屜式佈局的左側佈局
        app:headerLayout="@layout/drawer_header_layout"/左側放置頭像的佈局
        app:menu="@menu/drawer_menu"//左側放置選單的佈局
        app:itemIconTint="#ff00"//定義選單圖片顏色
        />
</android.support.v4.widget.DrawerLayout>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/left_head_background"
        android:scaleType="centerCrop"/>

    <ImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:padding="8dp"
        android:src="@drawable/default_avatar"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:padding="16dp"
        android:textColor="#000"
        android:textSize="17sp"
        android:text="魏群" />

</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_one"
            android:icon="@drawable/drawer_default_icon"
            android:title="檔案" />
        <item
            android:id="@+id/navigation_item_two"
            android:icon="@drawable/drawer_default_icon"
            android:title="目錄" />
        <item
            android:id="@+id/navigation_item_three"
            android:icon="@drawable/drawer_default_icon"
            android:title="佳佳" />
        <item
            android:id="@+id/navigation_item_four"
            android:icon="@drawable/drawer_default_icon"
            android:title="鬧鐘" />
        <item
            android:id="@+id/navigation_item_five"
            android:icon="@drawable/drawer_default_icon"
            android:title="設定" />


        <!--<item-->
            <!--android:id="@+id/navigation_subheader_four"-->
            <!--android:icon="@drawable/drawer_default_icon"-->
            <!--android:title="子條目">-->
            <!--<menu>-->
                <!--<item-->
                    <!--android:id="@+id/navigation_sub_item_eight"-->
                    <!--android:icon="@drawable/drawer_default_icon"-->
                    <!--android:title="佳佳" />-->
                <!--<item-->
                    <!--android:id="@+id/navigation_sub_item_nine"-->
                    <!--android:icon="@drawable/drawer_default_icon"-->
                    <!--android:title="鬧鐘" />-->
            <!--</menu>-->
        <!--</item>-->
    </group>
</menu>
3.使用

drawerLayout=findViewById(R.id.drawerlayout);
navigationview=findViewById(R.id.navigationview);
headerView = (FrameLayout) navigationview.getHeaderView(0);
navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_item_one:
                break;
            case R.id.navigation_item_two:
                break;
            case R.id.navigation_item_three:
                break;
            case R.id.navigation_item_four:
                break;
            case R.id.navigation_item_five:
                break;
        }
        return false;
    }
});
4.關閉側滑選單

drawerLayout.openDrawer(Gravity.LEFT);