lxx 的答案完美无缺。我无法粘贴所有代码作为响应,所以我提出了另一个答案。与 lxx 的代码唯一不同的是,我没有复制所有 SlidingDrawer 类,而是通过创建新类来扩展它。以下是完整代码。
package com.localini.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SlidingDrawer;
public class ExtendedSlidingDrawer extends SlidingDrawer {
private boolean mVertical;
private int mTopOffset;
public ExtendedSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
int orientation = attrs
.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
}
public ExtendedSlidingDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
int orientation = attrs
.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
}
@Override
protected void dispatchDraw(Canvas canvas) {
final long drawingTime = getDrawingTime();
final View handle = getHandle();
final boolean isVertical = mVertical;
drawChild(canvas, handle, drawingTime);
//if (mTracking || mAnimating) {
final Bitmap cache = getContent().getDrawingCache();
if (cache != null) {
if (isVertical) {
canvas.drawBitmap(cache, 0, handle.getBottom(), null);
} else {
canvas.drawBitmap(cache, handle.getRight(), 0, null);
}
} else {
canvas.save();
canvas.translate(isVertical ? 0 : handle.getLeft() - mTopOffset,
isVertical ? handle.getTop() - mTopOffset : 0);
drawChild(canvas, getContent(), drawingTime);
canvas.restore();
}
//} else if (mExpanded) {
//drawChild(canvas, mContent, drawingTime);
//}
}
}
在 xml 布局文件中,您需要设置例如android:buttonOffser="-100dip"
原始问题中的解释。