【发布时间】:2010-09-12 17:48:45
【问题描述】:
有没有办法让抽屉从上往下滑动?
【问题讨论】:
-
你可以看看github.com/Ali-Rezaei/SlidingDrawer,这让你可以从任何一侧滑行。
标签: android
有没有办法让抽屉从上往下滑动?
【问题讨论】:
标签: android
我找到了一个简单的方法来做到这一点。您所要做的就是为slidingDrawer、内容和手柄设置180º的旋转。举个例子更容易理解,看看我做了什么:
首先,我将向您展示我的旧 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="vertical"
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>
现在看看我所做的更改,设置旋转 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="vertical"
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 来设置为句柄,并没有改变它的旋转,但我改变了它的子元素的旋转。这是为了防止我遇到一个小问题,但一切正常,而且很简单。
【讨论】:
android:rotation 是 API 级别 11 (Android 3.0)。
默认的SlidingDrawer 类不允许这样做。您可以从这里使用Panel 类来获得非常相似的东西:
http://code.google.com/p/android-misc-widgets/
【讨论】:
我对这里提供的解决方案非常不满意:
Panel 类使用起来非常不直观,并且存在错误和视觉故障(无法用于生产用途),而且根本没有文档SlidingTray 类嵌套在需要过多依赖的库中,对我来说我根本没有让它工作android:rotation="180" 需要 API 级别 11,我的目标是 10。(无意冒犯各自的开发者,在这里尽量保持客观)
所以我的解决方案是从这个库中提取 SlidingTray http://aniqroid.sileria.com/doc/api/(由 Ahmed Shakil 提供)和
对其进行了一些重构,因为它需要在 Ahmed 的库中使用一些怪癖。 SlidingTray 基于 Android 自己的 SlidingDrawer,所以我猜它是稳定的。我的修改包括 1 个类,我称之为 MultipleOrientationSlidingDrawer 和
您必须在 attrs.xml 中添加声明样式。除此之外,它的用法与SlidingDrawer 几乎相同
带有额外的“方向”属性..
查看:MultipleOrientationSlidingDrawer (source & example) @ gist
这里是一个用法示例(也在要点中提供)
<your.app.MultipleOrientationSlidingDrawer
xmlns:custom="http://schemas.android.com/apk/res-auto/your.app"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:handle="@+id/handle_c"
custom:content="@+id/content_c"
custom:orientation="top">
<RelativeLayout
android:id="@id/handle_c"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#333333">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Handle Text"
android:gravity="left|center_vertical"/>
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@id/content_c"
android:background="#555555">
<ListView
android:id="@+id/listview_credits"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</your.app.MultipleOrientationSlidingDrawer>
免责声明:所有功劳归各自开发者所有。我没有对这个解决方案进行广泛的测试,效果很好 在 XML 中设置 TOP 和 BOTTOM。我没有尝试以编程方式使用它。
【讨论】:
content_c不占用所有可用空间,因为我看到它把所有东西都推倒了,即使我设置android:layout_height="wrap_content"
我不得不为我的一个项目做同样的事情,我最终为此编写了自己的小部件。我称它为 SlidingTray 现在是我的开源 Aniqroid 库的一部分。
http://aniqroid.sileria.com/doc/api/(在底部查找下载或使用谷歌代码项目查看更多下载选项:http://code.google.com/p/aniqroid/downloads/list)
课程文档在这里:http://aniqroid.sileria.com/doc/api/com/sileria/android/view/SlidingTray.html
【讨论】:
com.sileria.android.Resource.getIdentifier 得到一个nullpointer exception。很想用这个。
Kit.init()...现在很好用!
com.sileria.android.view.SlidingTray 而不是 com.sileria.android.SlidingTray