DrawLayout布局
<LinearLayout 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"
tools:context="com.example.day3.MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:background="#bdbebf"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主界面,必须先写-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<TextView
android:text="我是侧拉主界面"
android:textSize="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<!--侧滑部分 android:layout_gravity="left" 从左侧滑出-->
<LinearLayout
android:layout_gravity="left"
android:layout_width="200dp"
android:layout_height="match_parent"
android:gravity="center">
<ListView
android:id="@+id/lv"
android:background="#b49797"
android:layout_width="wrap_content"
android:layout_height="match_parent"></ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
DrawLayoutMainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout=findViewById(R.id.drawer_layout);
initToolBar();
bindToolBar();
}
private void initToolBar() {
toolbar = findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.mipmap.ic_launcher);
toolbar.setTitle("天气");
toolbar.setLogo(R.mipmap.ic_launcher_round);
toolbar.setSubtitle("Android");
}
}
SlidingMenuActiyity
public class SlidingMenuActivity extends AppCompatActivity {
private SlidingMenu slidingMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_menu);
initmenu();
}
private void initmenu() {
slidingMenu=new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setBehindOffset(200);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
View view = LayoutInflater.from(this).inflate(R.layout.slide, null);
Button button=view.findViewById(R.id.bt_close);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slidingMenu.showContent();
}
});
slidingMenu.setMenu(view);
slidingMenu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
@Override
public void onOpened() {
Toast.makeText(SlidingMenuActivity.this, "打开了", Toast.LENGTH_SHORT).show();
}
});
slidingMenu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
@Override
public void onClosed() {
Toast.makeText(SlidingMenuActivity.this, "侧滑菜单关闭", Toast.LENGTH_SHORT).show();
}
});
}
}