我正在处理由官方android库提供的导航抽屉。而且我从左侧快速移动,问题是默认触发区域太小,用户必须在非常左边触摸打开抽屉,有没有办法自定义尺寸,或放大尺寸?谢谢你的帮助。
答案 0 :(得分:2)
您可以这样做:
private void setLeftMargin() {
java.lang.reflect.Field mDragger = null;
try {
mDragger = mDrawerLayout.getClass()
.getDeclaredField("mLeftDragger");
} catch (NoSuchFieldException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
mDragger.setAccessible(true);
ViewDragHelper draggerObj = null;
try {
draggerObj = (ViewDragHelper) mDragger.get(mDrawerLayout);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.lang.reflect.Field mEdgeSize = null;
try {
mEdgeSize = draggerObj.getClass().getDeclaredField("mEdgeSize");
} catch (NoSuchFieldException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mEdgeSize.setAccessible(true);
int edge = 0;
try {
edge = mEdgeSize.getInt(draggerObj);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mEdgeSize.setInt(draggerObj, edge * 1);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
它会根据您的要求设置左侧区域。您可以设置int edge = valueYouNeed;
希望它会对您有所帮助。
答案 1 :(得分:0)
kotlin版本的AndiM答案
private fun setDrawerEdge(mDrawerLayout: DrawerLayout, isLeftDrawer: Boolean = true) {
try {
val manager = mActivity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val width = DisplayMetrics().also { manager.defaultDisplay.getMetrics(it) }.widthPixels
val viewDragHelper = mDrawerLayout::class.java
.getDeclaredField(if (isLeftDrawer) "mLeftDragger" else "mRightDragger")
.apply { isAccessible = true }
.run { get(mDrawerLayout) as ViewDragHelper }
viewDragHelper.let { it::class.java.getDeclaredField("mEdgeSize") }
.apply { isAccessible = true }
.apply { setInt(viewDragHelper, width/2) }
} catch (e: Exception) {
e.printStackTrace()
}
}