如何更改android滑动抽屉把手按钮位置

如何更改android滑动抽屉把手按钮位置,android,android-sliding,Android,Android Sliding,默认情况下,抽屉中央的android SlidingDrawer中的手柄按钮。是否可以将该位置更改为左或右。。?如果可能的话,我该怎么做呢?android:gravity=“left”在按钮中不起作用。将你的手柄放在相对位置,并在手柄上设置android:layout\u alignParentRight=“true” 例如: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http:

默认情况下,抽屉中央的android SlidingDrawer中的手柄按钮。是否可以将该位置更改为左或右。。?如果可能的话,我该怎么做呢?
android:gravity=“left”
在按钮中不起作用。

将你的手柄放在相对位置,并在手柄上设置android:layout\u alignParentRight=“true”

例如:

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

我尝试了
android:paddingBottom=“300dp”
来查看
ImageView
,它工作得很好,包括左、右和上。
但是您必须从中间位置设置填充,因此
bottom=“300dp”
意味着手柄从中间向上移动300

想法很简单:根据您想要的对齐方式,将手柄的填充设置为正确的值

在下面的示例中,我从SlidingDrawer派生了一个类,并重写onLayout,如下所示。我的滑动抽屉在屏幕的底部,我希望把手向左对齐,而不是居中对齐

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();
            }
        }
    }
}
公共类ToolDrawer扩展了SlidingDrawer{
private int m_handleWidth=0;//原始句柄宽度
公共布尔onTouchEvent(运动事件){
float x=event.getX();

如果(x我对HenrikS的回答是,在我的例子中,我在相对布局后面有一些视图,当我尝试单击它们时,我意识到相对布局拦截了这些单击,因为宽度设置为填充

为了解决这个问题,我在左边修改了处理程序视图(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);
}
}

左对齐手柄的更简单解决方案:

@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());
}

我为加里贝找到了解决办法
你只需要在滑动抽屉的内容布局上设置安卓:clickable=“true”。

我已经研究了两天这个问题,我意识到你需要使用另一个库。因为,当你使用RelativeLayout或LinearLayout时,你只需要填充父屏幕,当然触摸会影响它(尽管您不触摸图标)

因此,我使用Sileria library(3.5版)中的Slidengray


这个答案应该被接受——HenrikS的解决方案对我来说非常有效。对我来说不起作用。我可以在“handle”视图(或“handle”视图的子视图,如上所述)中指定一个“android:layout\u alignParentRight”属性,这样它可以编译,但不幸的是,在运行时它被忽略了(因此ImageView仍然是居中对齐的)。要添加:将RelativeLayout的宽度指定为“填充父对象”,即使代码运行了,屏幕的整个宽度也会充当手柄,我猜这不是我想要的,至少对我来说不是。这对一些人来说不起作用,因为他们忽略了这样一个事实:滑动抽屉的手柄现在应该是按钮的相对布局,而不是按钮id本身。这真的很奇怪(而我自己也不知道……)但是安卓布局更像是哈利波特魔法而不是编程…安卓布局不是哈利波特魔法。如果有人对如何实现这一点或如何修改滑动抽屉来实现这一点有任何建议,请寻找解决方案…嘿,我的滑块在屏幕的左侧,我希望滑动菜单出现从上到下,而不是从左到右;您能帮助我,在XML文件中必须做些什么吗。我非常感谢您的rply。如果我希望它位于右侧,我无法理解应该做哪些更改,请帮助