如何使Android SlidingDrawer从左侧滑出?

21

我在我的应用程序中使用了一个 slidingDrawer,在竖屏模式下它的处理程序位于底部。当用户切换到横屏模式(宽屏)时,我希望将处理程序定位在左侧。当我从竖直方向切换到水平方向时,处理程序会被放置在右侧。

我的布局XML定义如下:

<SlidingDrawer
    android:id="@+id/l_drawer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:handle="@+id/l_handle"
    android:content="@+id/l_content"
    android:orientation="horizontal"
    android:layout_gravity="left"
    >

有没有人有办法让它从左往右滑动?


实际上,我找到了一些实现类似功能的代码。博客带有屏幕截图: http://androidblogger.blogspot.com/2009/01/sliding-drawer-again.html 论坛讨论: http://www.anddev.org/viewtopic.php?p=16622 从SVN检出代码: http://code.google.com/p/android-misc-widgets/ - Vidar Vestnes
我也想要实现横向滑块,我已经检查了SVN的链接,但是没有任何可用的项目/代码。 - Paresh Mayani
点击页面顶部的“源”选项卡,然后选择“浏览”选项以显示源代码。 - Adil Hussain
4个回答

30

我找到了一个简单的方法来实现这个功能。你只需要将滑动抽屉(SlidingDrawer)的“rotation”属性设置为180度,同时也要对内容和手柄进行同样的设置。

你也可以像我在这里所做的那样,制作一个从顶部向下展开的滑动抽屉。

看看我的示例,在从右到左查看时,您将能够看到它们之间的区别。

<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/slidingDrawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:handle="@+id/handle"
    android:content="@+id/content">
    <ImageView android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <ImageView android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:src="@drawable/ic_launcher" />
</SlidingDrawer>

现在看看我所做的更改,使其从左侧滑出。

<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/slidingDrawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:handle="@+id/handle"
    android:content="@+id/content"
    android:rotation="180">
    <LinearLayout android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:rotation="180" />
    </LinearLayout>
    <ImageView android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:src="@drawable/ic_launcher"
        android:rotation="180" />
</SlidingDrawer>

注意我还创建了一个LinearLayout作为手柄,并且没有改变它的旋转,但是我改变了它的子项的旋转。这是为了防止我遇到的一个小问题,但一切正常且简单。


4
请注意,旋转功能适用于 API 级别 11+ (Honecomb)。 - Parth Mehrotra
1
请注意,SlidingDrawer在API 17(Android 4.2)中已被弃用,因此最好未雨绸缪,迁移到其他方案。 - spaaarky21
@spaaarky21,我想说的是,在2.2和4.2中都无法正常工作,所以你能否提供任何解决此任务的方法? - TheLittleNaruto
@Kumar 在看不到你的代码之前,我无法说出为什么它在4.2中对你不起作用。 至于2.2,如果你读过Parth Mehrotra在上面的评论,你会发现旋转直到Android 3才被支持。 就像我说的那样,最好完全迁移到SlidingDrawer以外,因为在3.0之前无法旋转抽屉,而SlidingDrawer在4.2中已被弃用。 在StackOverflow上有其他问题讨论替代方案。 - spaaarky21
如果您将滑动抽屉的内容放在单独的布局中,并将该布局旋转180度,则在Android 4.4上运行得非常出色! - dy_
显示剩余4条评论

6

除了获取SlidingDrawer源代码,进行更改并使用您修改后的版本之外,我认为您无法实现。同样,您也无法创建一个从顶部下降的SlidingDrawer


在进一步搜索后,我仍然没有找到任何示例或方法来做到这一点,所以是的,我同意,使用Android SDK无法实现这一点... - Vidar Vestnes

1

我重新编写了一个类,并将其作为我的开源库的一部分。我花了近一个星期的时间才把它写好。请查看我在 Android 的开源库 Aniqroid 中的 SlidingTray

http://aniqroid.sileria.com/doc/api

在上面的链接中找到SlidingTray类的下载链接和文档。

(声明:我是该项目的维护者。)


1
当调用SlidingTray时,我遇到了一个空指针异常。11-13 16:41:51.980: E/AndroidRuntime(23438): Caused by: java.lang.reflect.InvocationTargetException - vandus
我也遇到了空指针异常。java.lang.NullPointerException:com.sileria.android.view.SlidingTray.onLayout(Unknown Source) - Sathesh

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接