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

Miu*_*nga 13 android android-sliding

在抽屉中心的android SlidingDrawer中的默认句柄按钮.是否可以将该位置改为左侧或右侧..?如果可能的话,我怎么能这样做,android:gravity="left"按钮不起作用.

Hen*_*ikS 21

把你的句柄放在RelativeLayout中,并在句柄上设置android:layout_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>
Run Code Online (Sandbox Code Playgroud)

  • 对我不起作用.我_can_在'handle'视图中指定'android:layout_alignParentRight'属性(或'handle'视图的子节点,如上所述),这样它就可以编译但不幸的是它在运行时被忽略(因此ImageView仍然是居中对齐的) ).要添加:RelativeLayout的宽度指定为"fill_parent",即使代码确实运行,屏幕的整个宽度也将作为句柄,我猜测不是我想要的,至少对我来说不是. (4认同)

gar*_*bay 5

我对HenrikS回答的问题是,在我的情况下,我在相对布局背后有一些观点,当我尝试点击它们时,我意识到相对布局是拦截那些点击,因为宽度设置为fill_parent.

为了解决这个问题,我在左边的情况下修改了处理程序视图(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);
}
}
Run Code Online (Sandbox Code Playgroud)