抽屉是否有可能阻止儿童接收触摸事件:
这是我的xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- THE MENU -->
<RelativeLayout
android:id="staic_menu"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/mainDrawer">
<Button
android:layout_width="300dp"
android:layout_width="wrap_content"
android:text="Click Me"
/>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<!-- LEFT DRAWER -->
<RelativeLayout
android:id="@+id/drawerView"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:visibility="gone">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
当抽屉打开时,按钮会被忽略..但是当它关闭时,按钮会起作用。
谢谢,
答案 0 :(得分:2)
您需要使用自定义抽屉。
与默认区别不同:
以下代码有这些不利条件/限制:
DrawerLayout
的第二个孩子是ListView
(或其他一些抽屉内容)。<强> UnobtrusiveDrawer.java:强>
public class UnobtrusiveDrawer extends DrawerLayout {
public UnobtrusiveDrawer(Context context) {
super(context);
}
public UnobtrusiveDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UnobtrusiveDrawer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
View drawer = getChildAt(1);
Log.e("TAG", "Ev: " + ev.getRawX() + " " + drawer.getWidth());
if (isDrawerOpen(drawer) && ev.getRawX() > drawer.getWidth()) {
Log.e("TAG", "Drawer open, and click outside");
return false;
} else {
Log.e("TAG", "Drawer closed or click on it");
return true;
}
}
}
<强> MainActivity.java:强>
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
drawer.setScrimColor(getResources().getColor(android.R.color.transparent));
drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {}
@Override
public void onDrawerClosed(View drawerView) {}
@Override
public void onDrawerStateChanged(int newState) {}
});
}
}
<强> activity_main.xml中:强>
<com.example.myapplication.UnobtrusiveDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_gravity="right"
android:layout_width="100dp"
android:layout_height="100dp"/>
</FrameLayout>
<ListView
android:layout_gravity="left"
android:background="#FFF"
android:layout_width="250dp"
android:layout_height="match_parent"/>
</com.example.myapplication.UnobtrusiveDrawer>