现在的位置: 首页 > 综合 > 正文

android SlidingMenu 开发

2017年10月11日 ⁄ 综合 ⁄ 共 2957字 ⁄ 字号 评论关闭

最近发现越来越多的应用都增加了SlidingMenu功能,似乎现在这个很流行,于是上网找资料,研究研究这个东西怎么做出来的。

网上有许多写法,各不相同。

简单的方法是两个layout重叠摆放,然后使用手势识别类库来判断左右滑动及移动距离。

我使用的是GitHub上的SlidingMenu,开源的 library project,地址奉上:SlidingMenu资源下载地址https://github.com/jfeinstein10/SlidingMenu).

使用这个开源的 library 并运行资源中的demo,需要另外一个东西:ActionBarSherlock. 地址奉上:ActionBarSherlock地址(https://github.com/JakeWharton/ActionBarSherlock).

感谢网上的很多大牛,上面的东西都是从网上搜索的。

剩下的东西就原文连接:点击打开链接 http://blog.csdn.net/zoeice/article/details/8721137

zai

步骤:

1.将下载的Actionbarssherlock中/library下的文件解压到workspace(你的项目目录),改项目名称为Actionbarssherlock,从eclipse中操作new->project->Android Project from Existing
Code将项目添加进去,target改为api14以上。

PS:在引入Actionbarssherlock这个工程时,有可能会报"工程描述残缺",没关系,把工程放到workspace以外的目录,然后再Android
Project from Existing Code.

2.将下载的slidingmenu中/library解压到workspace(你的项目目录),改名SlidingMenu,import该项目,右键->properties->android->api14以上。或者打开project.properties:target=Google Inc.:Google
APIs:14或更高。

PS:import这个项目的正确做法,新建一个android项目,把creat activity什么的复选框全部勾去,即创建了一个空的项目,然后再import.

3.这样SlidingMenu就可以用在自己项目中了。让我们来运行例子。解压slidingmenu中/example。import该项目,设置api14或更高,并add你的SlidingMenu。运行看看效果。2.1以上的系统都可以运行。

虽然是开源项目,但使用过程中还是可能会出现各种问题:

一、修复SlidingMenu library project里出现的错误

  • Console error: Unable to resolve target ‘Google Inc.:Google APIs:16.

    Fix: 进入Properties检查Target sdk是否选择了 Google APIs.

  • List of errors: ACTION_POINTER_INDEX_MASK cannot be resolved android.
    HONEYCOMB cannot be resolved or is not a field
    LAYER_TYPE_HARDWARE cannot be resolved or is not a field
    LAYER_TYPE_NONE cannot be resolved or is not a field
    MATCH_PARENT cannot be resolved or is not a field
    The method getLayerType() is undefined for the type View
    The method setLayerType(int, null) is undefined for the type View

    Fix: 将manifest里面的min sdk version 改为你当前用到的sdk版本,7以上.

二、修复example project里出现的错误

  • Console error: Found 2 versions of android-support-v4.jar in the dependency list,but not all the versions are identical
    (check is based on SHA-1 only at this time). Jar mismatch! Fix your dependencies

    Fix: 这是由于jar包版本不一致导致的,检查SlidingMenu和Actionbarsherlock中的\libs文件夹里的哪个android-support-v4.jar版本比较新,用新的替换掉其中的老版本. 如果错误还是会出现,就clean项目并重启eclipse.

  • List of errors: The method getSupportActionBar() is undefined for the type BaseActivity
    The method getSupportMenuInflater() is undefined for the type BaseActivity
    The method onCreateOptionsMenu(Menu) of type BaseActivity must override or implement a supertype method
    The method onOptionsItemSelected(MenuItem) in the type Activity is not applicable for the arguments
    The method onOptionsItemSelected(MenuItem) of type BaseActivity must override or implement a supertype method
    The method onOptionsItemSelected(MenuItem) of type ResponsiveUIActivity must override or implement a supertype method

    Fix:打开SlidingMenu library project里SlidingFragmentActivity这个类并在声明前添加以下代码:

    [java] view
    plain
    copy

    1. import com.actionbarsherlock.app.SherlockFragmentActivity;  

    然后,将下面这个父类:

    [java] view
    plain
    copy

    1. public class SlidingFragmentActivity extends FragmentActivity implements SlidingActivityBase {  


    换成Actionbarsherlock里的另外一个父类:

    [java] view
    plain
    copy

    1. public class SlidingFragmentActivity extends SherlockFragmentActivity implements SlidingActivityBase {  


    如果错误还是会出现,就clean项目并重启eclipse.

附上运行截图:

 

 

 

抱歉!评论已关闭.