效果图
activity_main.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.support.v4.widget.DrawerLayout android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffffff"> <ImageView android:layout_width="match_parent" android:layout_height="355dp" android:background="@mipmap/ic_main_background" /> <RelativeLayout android:id="@+id/titleRelativeLayout" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginTop="18dp"> <FrameLayout android:id="@+id/userFrameLayout" android:layout_width="45dp" android:layout_height="match_parent"> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="center_vertical" android:layout_marginLeft="15dp" android:src="@mipmap/ic_user" /> </FrameLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="标题" android:textColor="#fff0f0f0" android:textSize="18sp" /> </RelativeLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:text="DrawerLayout演示" android:textColor="#fff0f0f0" android:textSize="25sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="500dp" android:text="This is a stub." android:textAllCaps="false" /> </RelativeLayout> <LinearLayout android:id="@+id/leftLinearLayout" android:layout_width="233dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#ffffffff" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="39dp" android:layout_marginBottom="23dp"> <ImageView android:layout_width="56dp" android:layout_height="56dp" android:layout_marginLeft="18dp" android:layout_marginRight="16dp" android:src="@mipmap/ic_portrait" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="布同林" android:textColor="#ff1b1b1b" android:textSize="18sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#ffdfdfdf" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="18dp" android:drawableLeft="@mipmap/ic_profile" android:drawablePadding="17dp" android:paddingTop="30dp" android:text="我的资料" android:textColor="#ff1b1b1b" android:textSize="14sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="18dp" android:drawableLeft="@mipmap/ic_message" android:drawablePadding="17dp" android:paddingTop="27dp" android:text="我的消息" android:textColor="#ff1b1b1b" android:textSize="14sp" /> <TextView android:id="@+id/aboutUsTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="18dp" android:drawableLeft="@mipmap/ic_information" android:drawablePadding="17dp" android:paddingTop="27dp" android:text="关于我们" android:textColor="#ff1b1b1b" android:textSize="14sp" /> <Button android:id="@+id/closeDrawerButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="40dp" android:text="关闭" /> </LinearLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout>
MainActivity.java
package com.bu_ish.empty; import android.graphics.Color; import android.os.Build; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.Window; public class MainActivity extends AppCompatActivity { private DrawerLayout drawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Window window = getWindow(); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.setStatusBarColor(Color.TRANSPARENT); } drawerLayout = findViewById(R.id.drawerLayout); findViewById(R.id.userFrameLayout).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { drawerLayout.openDrawer(Gravity.LEFT); } }); findViewById(R.id.aboutUsTextView).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(MainActivity.this).setMessage("布同林").show(); } }); findViewById(R.id.closeDrawerButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { drawerLayout.closeDrawer(Gravity.LEFT); } }); } }
P.S.
从左侧划出菜单,则android:layout_gravity="left"
DrawerLayout.openDrawer(int gravity):打开菜单
DrawerLayout.closeDrawer(int gravity):关闭菜单
完整Demo