MainActivity如下:
package com.wuzhiqiang.learnslidingmenu;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public class MainActivity extends AppCompatActivity {
private SlidingMenu slidingMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);//设置模式,在左边
slidingMenu.setBehindOffset(R.dimen.sliding_menu_offset);//设置出来的宽度
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//设置全屏都可触摸拖出
slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);//绑定当前activity
slidingMenu.setMenu(R.layout.sliding_menu);//绑定布局视图
}
//重写KeyDown方法,绑定菜单按键拉出SlidingMenu
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
slidingMenu.toggle(true);
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
sliding_menu.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff8990">
<com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/slidingmenu">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Check me!"
android:background="#0f0"/>
</com.jeremyfeinstein.slidingmenu.lib.SlidingMenu>
</LinearLayout>
androidStudio配置方法:把SlidingMenu_master中的Library,复制到项目下的libraries目录下,修改library的build.gradle,修改项目下的setting.gradle。
library下build.gradle如下:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
sourceSets {
main {
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
manifest.srcFile 'AndroidManifest.xml'
}
}
}
app下build.gradle如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.wuzhiqiang.learnslidingmenu"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
compile project(':library')
}
setting.gradle如下:
include ':app',':library'
project(':library').projectDir = new File('libraries/library')
运行效果如下:
问题解决了!
因为我之前是手动把SlidingMenu中的library添加到项目路径下,再修改配置文件,这中间不知道出了什么问题。
昨天我重新写了这个程序,让AS自动添加librayr,然后修改一些配置,它就行了!!1