关于AndriodStudio导入问题—以Github开源代码SildingMenu为例

来源:互联网 发布:js将base64转换为图片 编辑:程序博客网 时间:2024/06/05 05:38

关于AndriodStudio导入问题—以Github开源代码SildingMenu为例

注:这个问题让我这个AndriodStudion新手浪费了半天时间,主要是网上已有的解决方法多,且版本较低,大部分已经无法一次解决新手遇到的全部问题,在此写个博客,一做总结,二也为后来使用者作参考。


首先,我只是在跟同学开发的项目中偶然用到了GitHub的开源项目SlidingMenu,这是一款非常帅气的控件——侧滑菜单,符合UI的高度流畅性的特点,在我看来也是手机应用制作菜单栏目的上上选,单击什么的太low了,因为是中途加入这个项目,所以已经是个半成品了,同学直接把工程代码给我了,但是呢,他用的是eclipse ADT,然后拷贝的时候并不了解eclipse还可以生成AndriodStudio的工程文件,好像是在Export窗口中选择“Android”下面的Generate Gradle build files就ok了,可是我刚开始不知道啊,现在你们知道了。于是我又懒得麻烦人家,但是直接导入eclipse工程是很危险的,所以就随手创建了一个,对,人工重构!!虽然代码可以复制的,但是还是有很多东西要改啊,是吧。不讲废话了,进入正题:

就在第一个Activity我遇到了SlidingMenu,当时不知道什么鬼,google了一下,才知道是GitHub的开源项目,目测就是那个叫jeremyfeinstein的大神写的框架。然后呢我开始下载官网的代码,还听说里面会用到另一款叫做Actionbarsherlock的开源项目,我看我的工程里面并没有用到于是就没有理会。

SildingMenu下载地址:https://github.com/jfeinstein10/slidingmenu
Actionbarsherlock下载地址:http://actionbarsherlock.com

导入按下列步骤进行:

1、将下载的文件SlidingMenu-master.zip解压并且重命名为SlidingMenu(为了使用方便)。

2、原目录SlidingMenu-master下的library重命名为SlidingMenu(同样为了使用方便)。

3、创建或打开Andriod中的工作空间,注:eclipse中工程是Project,AndriodStudio中Project相当于工作空间。在此我重新创建了一个Project并且命名为SlidingMenu,一直next,并且创建一个Empty Activity。

4、右击要用到SlidingMenu的项目:如app,选择Show In Explorer,打开项目根目录,创建一个空文件夹命名为libraries,并
将解压后的SlidingMenu(SlidingMenu-master.zip解压的全部文件,不能少,否则使用时会报错)文件复制到libraries文件
中。

5、点击AS左上角的File,选择New->Import Module(Module相当于eclipse中的Project),导入SlidingMenu下的SlidingMenu。(例如:AndroidStudioProjects\SlidingMenu(项目名)\libraries\SlidingMenu\SlidingMenu)


6、这时候会报错:Error:The SDK Build Tools revision (17.0.0) is too low for project ':SlidingMenu'. Minimum required is 19.1.0,意思是SDK版本对于本工程来说太低,最低要使用19.1.0,我们可以在19.1.0和本工程SDK版本之间选合法的值,不过我这里推荐使用本工程的SDK版本。打开SlidingMenu根目录AndroidStudioProjects\SlidingMenu(项目名)\libraries\SlidingMenu\SlidingMenu找到build.gradle文件,将工程中的build.gradle文件(Module:app下的)中的Andriod和defalutConfig进行修改如图:拷贝代码部分代码如下:


<strong>android {    compileSdkVersion 23    buildToolsVersion "23.0.3"    defaultConfig {        applicationId "com.zhuyueming.slidingmenu"        minSdkVersion 15        targetSdkVersion 23        versionCode 1        versionName "1.0"    }</strong>

7、覆盖SlidingMenu下的build.gradle文件中的相应部分(笔者使用editPlus进行修改)。然后点击同步失败提示中的TryAgain。紧接着会报第二个错:Error:Library projects cannot set applicationId. applicationId is set to 'com.zhuyueming.slidingmenu' in default config.意思是没法更改ApplicationId,打开Module:SlidingMenu下的build.gradle文件删除defalu中的ApplicatioionId:

<span style="color: rgb(51, 51, 51); font-family: Consolas; font-size: 12pt; line-height: 28px;">applicationId </span><span style="font-family: Consolas; font-size: 12pt; line-height: 28px; color: rgb(0, 128, 0); "><strong>"com.zhuyueming.slidingmenu"</strong></span>

修改dependencies 下的classpath如下:

dependencies {    classpath 'com.android.tools.build:gradle:1.1.0'}
8、完成上述修改后再次TryAgain。紧接着会报第三个错:

float distanceInfluenceForSnapDuration(float f) {   f -= 0.5f; // center the values about 0.   f *= 0.3f * Math.PI / 2.0f;   return (float) FloatMath.sin(f);}
Error:(303, 27) 错误: 找不到符号
符号:   方法 sin(float)
位置: 类 FloatMath

9、将FloatMath改为Math即可。这时已经完成了将SlidingMenu项目代码导入到appModule中使用,接下来编辑代码开始测试:遇到问题截图留言即可。

<span style="color: rgb(51, 51, 51);">package com.zhuyueming.slidingmenu;//</span><span style="color:#ff0000;">MainActivity.java</span><span style="color:#333333;">import android.app.Activity;import android.os.Bundle;import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;public class MainActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        SlidingMenu menu = new SlidingMenu(this);        menu.setMode(SlidingMenu.LEFT);        // 设置触摸屏幕的模式        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);        menu.setShadowWidthRes(R.dimen.shadow_width);        menu.setShadowDrawable(R.drawable.shadow);        // 设置滑动菜单视图的宽度        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);        // 设置渐入渐出效果的值        menu.setFadeDegree(0.35f);        /**         * SLIDING_WINDOW will include the Title/ActionBar in the content         * section of the SlidingMenu, while SLIDING_CONTENT does not.         */        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);        //为侧滑菜单设置布局        menu.setMenu(R.layout.slidingmenu);    }}</span>
<span style="color:#333333;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"//</span><span style="color:#ff0000;">activity_main.xml</span><span style="color:#333333;">    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/id_main_ly"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceLarge"        android:text="Sliding->"        android:id="@+id/textView"        android:layout_centerVertical="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" /></RelativeLayout></span>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"//<span style="color:#ff0000;">slidingmenu.xml</span>    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/id_main_ly"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceLarge"        android:text="Sliding->"        android:id="@+id/textView"        android:layout_centerVertical="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>//<span style="color:#ff0000;">shadow.xml</span><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <gradient        android:endColor="#00000000"        android:centerColor="#2D000000"        android:startColor="#5A000000" /></shape>
<resources>//<span style="color:#ff0000;">dimens.xml</span>    <!-- Default screen margins, per the Android Design guidelines. -->    <dimen name="activity_horizontal_margin">16dp</dimen>    <dimen name="activity_vertical_margin">16dp</dimen>    <dimen name="shadow_width">16dp</dimen>    <dimen name="slidingmenu_offset">16dp</dimen></resources>


文件位置参照:C:\Document\ZhuYueMing\AndroidStudioProjects\SlidingMenu\app\src\main\java\com\zhuyueming\slidingmenu\MainActivity.javaC:\Document\ZhuYueMing\AndroidStudioProjects\SlidingMenu\app\src\main\res\layout\activity_main.xmlC:\Document\ZhuYueMing\AndroidStudioProjects\SlidingMenu\app\src\main\res\layout\slidingmenu.xmlC:\Document\ZhuYueMing\AndroidStudioProjects\SlidingMenu\app\src\main\res\drawable\shadow.xmlC:\Document\ZhuYueMing\AndroidStudioProjects\SlidingMenu\app\src\main\res\values\dimens.xml


运行结果截图(没时间做成gif~):


1 0
原创粉丝点击