package com.example.drawerlayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public class Main2Activity extends AppCompatActivity {
private SlidingMenu slidingMenu;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2);
mToolbar = findViewById(R.id.toolbar);
mToolbar.inflateMenu(R.menu.menu);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.item1:
Toast.makeText(Main2Activity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(Main2Activity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(Main2Activity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(Main2Activity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
slidingMenu = new SlidingMenu(this);
slidingMenu.setMenu(LayoutInflater.from(this).inflate(R.layout.slideing,null));
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// slidingMenu.
slidingMenu.getMenu().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
slidingMenu.showContent();
}
});
}
public void click(View view) {
slidingMenu.showMenu();
}
}
//DrawerLayout的使用
package com.example.drawerlayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
public class Main3Activity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mDrawerLayout = findViewById(R.id.drawer_layout);
}
public void click(View view) {
switch (view.getId()){
case R.id.close:
mDrawerLayout.closeDrawers();
break;
case R.id.open:
mDrawerLayout.openDrawer(Gravity.LEFT);
break;
}
}
}
07-20