【问题标题】:Android SlidingDrawer from top?Android SlidingDrawer 从顶部?
【发布时间】:2010-09-12 17:48:45
【问题描述】:

有没有办法让抽屉从上往下滑动?

【问题讨论】:

标签: android


【解决方案1】:

我找到了一个简单的方法来做到这一点。您所要做的就是为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)。
  • 当我使用上述布局运行应用程序时,错误显示错误:在包'android'中找不到属性'rotation'的资源标识符
  • SlidingDrawer 在 API 级别 17 中已弃用
  • 这是我在 stackoverflow 中的最佳答案 :) 非常感谢
【解决方案2】:

默认的SlidingDrawer 类不允许这样做。您可以从这里使用Panel 类来获得非常相似的东西: http://code.google.com/p/android-misc-widgets/

http://www.ohloh.net/p/android-misc-widgets

【讨论】:

  • 您的面板是否正常工作?我很难集成该面板,但这似乎很酷。我的问题在这里,如果有人可以查看它code.google.com/p/android-misc-widgets/issues/detail?id=9 错误说“二进制 XML 文件第 15 行:内容属性是必需的,并且必须引用一个有效的孩子。”
  • 我必须将面板小部件包装到框架布局中才能使其正常工作
  • 顶部滴管有不良行为:下面的内容已移至底部。我这样修复它:
  • 面板实现不适用于 HTC One 等手机和其他 4.4 设备。
【解决方案3】:

我对这里提供的解决方案非常不满意:

(无意冒犯各自的开发者,在这里尽量保持客观)

所以我的解决方案是从这个库中提取 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。我没有尝试以编程方式使用它。

【讨论】:

  • 很好,但是你看到动画有问题吗?您是否找到了在移动时闪烁的解决方案?
  • 我确实有 code.google.com/p/android-misc-widgets 的闪烁动画,但没有这个 apporach(到目前为止)
  • 是否有可能使`FrameLayout`content_c不占用所有可用空间,因为我看到它把所有东西都推倒了,即使我设置android:layout_height="wrap_content"
【解决方案4】:

我不得不为我的一个项目做同样的事情,我最终为此编写了自己的小部件。我称它为 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

【讨论】:

  • 我试过这个,但是当我使用 xml 示例实现它时,我在com.sileria.android.Resource.getIdentifier 得到一个nullpointer exception。很想用这个。
  • 糟糕,我好像忘记了Kit.init()...现在很好用!
  • 另外,如果您从文档中复制/粘贴 XML 示例,它是 com.sileria.android.view.SlidingTray 而不是 com.sileria.android.SlidingTray
  • 它也无法正常工作并通过 NullPointerException @Peterdk !!
  • 您的 Kit.destroy() 方法不起作用。 onTerminate() 仅在模拟器中不会在真实设备上被调用。当应用程序“终止”时,没有真正的方法来获得回调,因为 android 范式不能那样工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多