In default handle button in android SlidingDrawer in the centre of the drawer. 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.
7 に答える
Put your handle in a RelativeLayout, and set android:layout_alignParentRight="true" on the handle.
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>
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);
}
}
考え方は単純です。必要な配置に応じて、ハンドルのパディングを正しい値に設定します。
以下の例では、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();
}
}
}
}
それはそれと同じくらい簡単です。
I tried android:paddingBottom="300dp"
for the ImageView
and it works fine, also with left, right and top.
But you have to set the padding from the middle-position, so bottom="300dp"
means the handle move up 300 from the middle
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. 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 ).
So, I use SlidingTray from Sileria library ( ver 3.5 ) http://aniqroid.sileria.com/doc/api/
I found the solution for garibay You just need to set android:clickable="true" on content layout of sliding drawer.