简体   繁体   English

SlidingDrawer打开时如何隐藏句柄?

[英]How to hide handle when SlidingDrawer is open?

Is it possible to show handle when drawer is closed and hide it when drawer is open ? 抽屉关闭时是否可以显示手柄,抽屉打开时是否可以隐藏?

I've tried overriding SlidingDrawer but had no success so far. 我已经尝试重写SlidingDrawer但到目前为止没有成功。

To hide handle button following trick worked for me 隐藏句柄按钮后面的技巧为我工作

    <Button
      android:id="@+id/handle"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:background="#00000000" />

you can use slidingdrawer's topOffset attribute with a negative value in dp. 你可以在dp中使用slidedrawer的topOffset属性和负值。 (the size of your handle) like so: (手柄的大小)如下:

android:topOffset="-50dp"

this way when you open the sliding drawer the handle will go pass the top screen making it gone from the view. 这样,当您打开滑动抽屉时,手柄将通过顶部屏幕,使其从视图中消失。 now for a cleaner look you just have to create a transparent space between the handle and the content layout of your sliding drawer. 现在为了更清洁的外观,您只需在手柄和滑动抽屉的内容布局之间创建一个透明空间。 you can do this by using a linearlayout with a transparent background assigned as your slidingdrawer's "android:content" layout and put your 'real' content layout inside and assigning a topMargin to it. 你可以通过使用一个带有透明背景的linearlayout作为你的slidedrawer的“android:content”布局来实现这一点,并将你的'真实'内容布局放入其中并为其分配topMargin。 looks something like this: 看起来像这样:

<LinearLayout
   android:id="@+id/transparent_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/transparent" >
     <LinearLayout
        android:id="@+id/real_content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp" >
     </LinearLayout>
</LinearLayout>

and here's how the whole xml layout for your sliding drawer with "hiding" / "overshot" handle would look like: 以下是带有“隐藏”/“过冲”手柄的滑动抽屉的整个xml布局如下所示:

<SlidingDrawer
    android:id="@+id/slidingdrawer_no_handle_when_opened"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:content="@+id/hiding_handle"
    android:handle="@+id/transparent_layout"
    android:orientation="vertical"
    android:topOffset="-50dp" >
    <LinearLayout
        android:id="@+id/hiding_handle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/buttonTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hiding Handle" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/transparent_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/transparent" >
        <LinearLayout
            android:id="@+id/real_content_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="20dp" >
        </LinearLayout>
    </LinearLayout>
</SlidingDrawer>

*note - you need to adjust the value for topOffset and topMargin n accordance to you actual layout. *注意 - 您需要根据实际布局调整topOffset和topMargin的值。 and you need to provide another way for the user to close the sliding drawer since your handle is hidden when opened right? 并且你需要提供另一种方式让用户关闭滑动抽屉,因为你的手柄在打开时是隐藏的吗? ;) ;)

To hide the handle set the alpha to 0 and to show it set the alpha to 255. 要隐藏句柄,请将alpha设置为0并显示它将alpha设置为255。

handle.setAlpha(0); // hide

handle.setAlpha(255); // show

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM