如何让 Android SlidingDrawer 从左侧滑出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1137350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:44:24  来源:igfitidea点击:

How to make an Android SlidingDrawer slide out from the left?

androidlayoutandroid-widgetslidingdrawer

提问by Vidar Vestnes

I'm using a slidingDrawerin my application that has its handler placed at the bottom when in portrait mode. When the user switches to landscape mode (widescreen) I would like to have the handler located on the left. When I change the orientation from vertical to horizontal, the handler is placed on the right.

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

I have defined my layout XML like this:

我已经像这样定义了我的布局 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"
    >

Anyone have an idea for how to make it slide from left to right ?

任何人都知道如何让它从左向右滑动?

采纳答案by CommonsWare

I don't think you can, other than perhaps by grabbing the SlidingDrawersource code, making changes to it, and using your modified version. You similarly cannot make a SlidingDrawerthat descends from the top.

我不认为你可以,除了可能通过获取SlidingDrawer源代码,对其进行更改并使用修改后的版本。你同样不能制作一个SlidingDrawer从顶部下降的。

回答by Leandroid

I've found a simple way to do that. All you have to do is to set the rotation of 180o for the slidingDrawer, the content and the handle. You can similarly make a SlidingDrawer that descends from the top, like I did here.

我找到了一个简单的方法来做到这一点。你所要做的就是为slidingDrawer、内容和手柄设置180o的旋转。您可以类似地制作一个从顶部下降的 SlidingDrawer,就像我在这里所做的那样。

Look at my examples here, first from right to left, to be able to see the differences.

看看我这里的例子,首先从右到左,以便能够看到差异。

<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>

Now look what I changed to make it sliding out from the left.

现在看看我改变了什么让它从左边滑出来。

<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>

Note that I also created a LinearLayout to set as handle, and didn't change it's rotation, but I changed the rotation of it's child. This was to prevent a small issue I had, but everything is working fine and it's simple.

请注意,我还创建了一个 LinearLayout 来设置为句柄,并没有改变它的旋转,但是我改变了它的子级的旋转。这是为了防止我遇到的一个小问题,但一切正常,而且很简单。

回答by Sileria

I have rewritten a class to that and made it part of my open source library. It took me almost a week to get it right. Please check out my SlidingTrayin Aniqroidopen source library for Android.

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

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

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

Find the download link and the documentation for SlidingTray class in the above link.

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

(Disclosure: I am the maintainer of the project.)

(披露:我是该项目的维护者。)

回答by Patrick Favre

You can use the code posted in this answer: Android SlidingDrawer from top?

您可以使用此答案中发布的代码:Android SlidingDrawer from top?

The provided solution features setting the orientation of the Slidingdrawer in xml also it's simple requiring only 1 class and some additions in attrs.xml and stable since it's derived from Androids Slidingdrawer from SDK. I also discuss why I didn't choose other popular libs/solutions found on the internet/SO.

提供的解决方案的特点是在 xml 中设置 Slidingdrawer 的方向,而且它很简单,只需要 1 个类和 attrs.xml 中的一些添加项,并且稳定,因为它源自 SDK 的 Androids Slidingdrawer。我还讨论了为什么我没有选择在 Internet/SO 上找到的其他流行的库/解决方案。

Quicklink to the gist: MultipleOrientationSlidingDrawer (source & example) @ gist

要点的快速链接:MultipleOrientationSlidingDrawer(源代码和示例)@gist