当前位置:   article > 正文

修改DrawerLayout只能从屏幕边缘划出的方法_drawerlayout setdrawershadow

drawerlayout setdrawershadow

方法来自Stack OverFlow,利用反射,修改DrawerLayout的侧滑菜单的感应范围,代码如下:

  1. public class MainActivity extends Activity {
  2. private DrawerLayout mDrawerLayout;
  3. private ListView mDrawerList;
  4. private ActionBarDrawerToggle mDrawerToggle;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  10. mDrawerList = (ListView) findViewById(R.id.left_drawer);
  11. // set a custom shadow that overlays the main content when the drawer opens
  12. mDrawerLayout.setDrawerShadow(R.drawable.your_drawer_shadow, GravityCompat.START);
  13. // set up the drawer's list view with items and click listener
  14. mDrawerList.setAdapter(new ArrayAdapter<String>(this,
  15. R.layout.your_drawer_list, yourItems));
  16. mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
  17. // enable ActionBar app icon to behave as action to toggle nav drawer
  18. getActionBar().setDisplayHomeAsUpEnabled(true);
  19. getActionBar().setHomeButtonEnabled(true);
  20. //关键代码
  21. Field mDragger = null;
  22. try {
  23. mDragger = mDrawerLayout.getClass().getDeclaredField(
  24. "mLeftDragger"); //mRightDragger for right obviously
  25. } catch (NoSuchFieldException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. }
  29. mDragger.setAccessible(true);
  30. ViewDragHelper draggerObj = null;
  31. try {
  32. draggerObj = (ViewDragHelper) mDragger
  33. .get(mDrawerLayout);
  34. } catch (IllegalArgumentException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. } catch (IllegalAccessException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. Field mEdgeSize = null;
  42. try {
  43. mEdgeSize = draggerObj.getClass().getDeclaredField(
  44. "mEdgeSize");
  45. } catch (NoSuchFieldException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. mEdgeSize.setAccessible(true);
  50. int edge = 0;
  51. try {
  52. edge = mEdgeSize.getInt(draggerObj);
  53. } catch (IllegalArgumentException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. } catch (IllegalAccessException e) {
  57. // TODO Auto-generated catch block
  58. e.printStackTrace();
  59. }
  60. try {
  61. mEdgeSize.setInt(draggerObj, edge * 5); //optimal value as for me, you may set any constant in dp
  62. //You can set it even to the value you want like mEdgeSize.setInt(draggerObj, 150); for 150dp
  63. } catch (IllegalArgumentException e) {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. } catch (IllegalAccessException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }
  70. // ActionBarDrawerToggle ties together the the proper interactions
  71. // between the sliding drawer and the action bar app icon
  72. mDrawerToggle = new ActionBarDrawerToggle(
  73. this, /* host Activity */
  74. mDrawerLayout, /* DrawerLayout object */
  75. R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
  76. R.string.drawer_open, /* "open drawer" description for accessibility */
  77. R.string.drawer_close /* "close drawer" description for accessibility */
  78. ) {
  79. public void onDrawerClosed(View view) {
  80. }
  81. public void onDrawerOpened(View drawerView) {
  82. }
  83. };
  84. mDrawerLayout.setDrawerListener(mDrawerToggle);
  85. }
  86. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/279498
推荐阅读
相关标签
  

闽ICP备14008679号