简体   繁体   English

如何更改android slidedrawer句柄按钮位置

[英]How to change android slidingdrawer handle button position

In default handle button in android SlidingDrawer in the centre of the drawer. 在抽屉中心的android SlidingDrawer中的默认句柄按钮。 Is it possible to change that position to left or right..? 是否可以将该位置改为左侧或右侧..? If it possible how can I do that, android:gravity="left" is not work in the button. 如果有可能我该怎么做, android:gravity="left"在按钮中不起作用。

Put your handle in a RelativeLayout, and set android:layout_alignParentRight="true" on the handle. 把你的句柄放在RelativeLayout中,并在句柄上设置android:layout_alignParentRight =“true”。

For example like this: 例如这样:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <SlidingDrawer android:id="@+id/drawer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:handle="@+id/handle" 
        android:content="@+id/content">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:id="@+id/handle">
            <ImageView android:src="@drawable/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true" />
        </RelativeLayout>

        <LinearLayout 
            android:gravity="center" 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#aaaa00" android:id="@+id/content">
            <ImageView android:src="@drawable/icon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center">
            </ImageView>
        </LinearLayout>

    </SlidingDrawer>

</FrameLayout>

The problem I had with HenrikS answer was that in my case I had some views behind the relative layout and when I tried to click them I realized that the relative layout was intercepting those clicks because the width was set to fill_parent. 我对HenrikS回答的问题是,在我的情况下,我在相对布局背后有一些观点,当我尝试点击它们时,我意识到相对布局是拦截那些点击,因为宽度设置为fill_parent。

To workaround this I modified the layout of the handler view (the ImageView) in my case to the left, hope it is helpful. 为了解决这个问题,我在左边的情况下修改了处理程序视图(ImageView)的布局,希望它有用。

public class CustomSlidingDrawer extends SlidingDrawer {

public CustomSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public CustomSlidingDrawer(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    final int height = b - t;

    ImageView handle = (ImageView) getHandle();
    int childLeft = 0;
    int childWidth = handle.getWidth();
    int topOffset = 0;
    int bottomOffest = 0;
    int childHeight = handle.getHeight();

    int childTop = this.isOpened()?topOffset:height - childHeight + bottomOffest;

    handle.layout(childLeft, childTop , childLeft + childWidth, childTop+childHeight);
}
}

The idea is simple: set the handle's padding to the correct value depending on the alignment you want. 这个想法很简单:根据您想要的对齐方式将手柄的填充设置为正确的值。

In the example below, I derive a class from SlidingDrawer and override onLayout as follows. 在下面的示例中,我从SlidingDrawer派生一个类并重写onLayout,如下所示。 My sliding drawer is at the bottom of the screen, and I want the handle to be left-aligned rather than center-aligned. 我的滑动抽屉位于屏幕的底部,我希望手柄左对齐而不是中心对齐。

public class ToolDrawer extends SlidingDrawer {
    private int m_handleWidth = 0;  // original handle width

    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        if (x <= m_handleWidth) {
            return super.onTouchEvent(event);
        }
        return false;
    }

    protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (changed) {
            ImageView iv = (ImageView) getHandle();
            iv.setPadding(0, 0, getWidth() - iv.getWidth(), 0);
            if (m_handleWidth == 0) {
                m_handleWidth = iv.getWidth();
            }
        }
    }
}

It's as simple as that. 就这么简单。

A simpler solution for left-aligned handle: 左对齐句柄的简单解决方案:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    final View handle = getHandle();
    handle.layout(0, handle.getTop(), handle.getWidth(), handle.getBottom());
}

I have researched this problem for 2 days and I realized that you need to use another library. 我已经研究了这个问题2天了,我意识到你需要使用另一个库。 Because, when you use RelativeLayout or LinearLayout, you're just get fill_parent screen and of course it will be affected by touching ( although you don't touch on icon ). 因为,当你使用RelativeLayout或LinearLayout时,你只是得到fill_parent屏幕,当然它会受到触摸的影响(尽管你没有触摸图标)。

So, I use SlidingTray from Sileria library ( ver 3.5 ) http://aniqroid.sileria.com/doc/api/ 所以,我使用Sileria库中的SlidingTray(版本3.5) http://aniqroid.sileria.com/doc/api/

Then like this 然后这样

I tried android:paddingBottom="300dp" for the ImageView and it works fine, also with left, right and top. 我为ImageView尝试了android:paddingBottom="300dp" ,它工作正常,也有左,右和顶部。 But you have to set the padding from the middle-position, so bottom="300dp" means the handle move up 300 from the middle 但你必须从中间位置设置填充,所以bottom="300dp"意味着手柄从中间向上移动300

我找到了garibay的解决方案你只需要在滑动抽屉的内容布局上设置android:clickable =“true”。

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

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