侧滑菜单(四)

此侧滑菜单是android.support.v4.widget包下自带的一种侧滑

下面看整体布局activity_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/activity_drawer_layout">
<!- 这是主界面->
    <LinearLayout
        android:id="@+id/activity_drawer_main_layout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/loading_bg"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="切换菜单" 
            android:id="@+id/activity_drawer_btn"/>
    </LinearLayout>
<!- 这是左侧菜单(android:layout_gravity="start"该属性表示在左侧滑出,如果改为“end”则为右侧滑出)->
    <include
        android:id="@+id/activity_drawer_side_menu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        layout="@layout/view_side_menu"
        android:background="@drawable/main_bg" />

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

DrawerActivity.class(改侧滑菜单是在屏幕边缘滑动才会出现,滑出后点击空白地方方可隐藏)

/**
 * 
 */
package com.test.sildingmenu;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * @author wanggenping
 *
 */
public class DrawerActivity extends Activity{
    private DrawerLayout layout;
    private LinearLayout leftMenu;
    private Button btn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        layout = (DrawerLayout) findViewById(R.id.activity_drawer_layout);
        leftMenu = (LinearLayout) findViewById(R.id.activity_drawer_side_menu);
        btn = (Button) findViewById(R.id.activity_drawer_btn);
        
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                layout.openDrawer(leftMenu);
            }
        });
        
//        layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);//关闭手势滑动
    }
}

view_side_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center_vertical"
    android:background="@drawable/main_bg">
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view_side_menu_one_layout"
        android:padding="15dp">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_image1"
            android:src="@drawable/icon_base_info"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_text1"
            android:layout_toRightOf="@+id/view_side_menu_image1"
            android:layout_marginLeft="7dp"
            android:text="第一个item"
            android:layout_centerVertical="true"
            android:textColor="#fff"
            android:textSize="20sp"/>
    </RelativeLayout>
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view_side_menu_two_layout"
        android:padding="15dp">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_image2"
            android:src="@drawable/icon_borrow_description"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_text2"
            android:layout_toRightOf="@+id/view_side_menu_image2"
            android:layout_marginLeft="7dp"
            android:text="第二个item"
            android:layout_centerVertical="true"
            android:textColor="#fff"
            android:textSize="20sp"/>
    </RelativeLayout>
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view_side_menu_three_layout"
        android:padding="15dp">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_image3"
            android:src="@drawable/icon_borrow_user_info"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_text3"
            android:layout_toRightOf="@+id/view_side_menu_image3"
            android:layout_marginLeft="7dp"
            android:text="第三个item"
            android:layout_centerVertical="true"
            android:textColor="#fff"
            android:textSize="20sp"/>
    </RelativeLayout>
    

</LinearLayout>


你可能感兴趣的:(侧滑菜单)