本文介绍了为什么我不能用重力布局锁定DrawerLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用DrawerLayout,最近我想改变的ListView的重心在drawerLayout。但之后,我改变的ListView重力的android:layout_gravity =开始|底的android:layout_gravity =开始,drawerLayout不能锁定到

I use DrawerLayout and recently i want to change gravity of listView in drawerLayout. But after i change gravity of listView to android:layout_gravity="start|bottom"from android:layout_gravity="start", drawerLayout can't be lock to

mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

setDrawerLockMode()一起工作;

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

<ListView
    android:id="@+id/drawer_list"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#F3F3F4"
    android:choiceMode="singleChoice" >
</ListView>

不过,这并不与锁定;

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

<ListView
    android:id="@+id/drawer_list"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_gravity="start|bottom"
    android:background="#F3F3F4"
    android:choiceMode="singleChoice" >
</ListView>

`

为什么我不能用锁定模式与其他比重任何线索?

Any clues of why can't I use lock mode with other gravities?

谢谢!

推荐答案

根据在,在的可使用的可用比重为 Gravity.LEFT Gravity.RIGHT GravityCompat.START GravityCompat.END

Based on the documentation, the only available gravities that can be used are Gravity.LEFT, Gravity.RIGHT or GravityCompat.START, GravityCompat.END.

(重点煤矿):

抽屉定位和布局是使用控制
  安卓:对孩子的看法layout_gravity属性对应于
  视图的一面你想要的抽屉,从出现:
  (或开始/结束在支持布局方向平台版本

纵观source code

public void setDrawerLockMode(int lockMode, int edgeGravity) {
  final int absGrav = GravityCompat.getAbsoluteGravity(edgeGravity,
                                                       ViewCompat.getLayoutDirection(this));
  if (absGrav == Gravity.LEFT) {
    mLockModeLeft = lockMode;
  } else if (absGrav == Gravity.RIGHT) {
    mLockModeRight = lockMode;
  }
  if (lockMode != LOCK_MODE_UNLOCKED) {
    // Cancel interaction in progress
    final ViewDragHelper helper = absGrav == Gravity.LEFT ? mLeftDragger : mRightDragger;
    helper.cancel();
  }
  switch (lockMode) {
    case LOCK_MODE_LOCKED_OPEN:
      final View toOpen = findDrawerWithGravity(absGrav);
      if (toOpen != null) {
        openDrawer(toOpen);
      }
      break;
    case LOCK_MODE_LOCKED_CLOSED:
      final View toClose = findDrawerWithGravity(absGrav);
      if (toClose != null) {
        closeDrawer(toClose);
      }
      break;
      // default: do nothing
  }
}

该方法本身只检查是否重力(但使用 GravityCompat 方法,因此开始 END 应适当翻译)。

The method itself only checks if the gravity is LEFT or RIGHT (but uses a GravityCompat method, so START and END should be appropriately translated).

这将意味着,通过设置的引力开始|底,你推出一个无效的严重性,这将导致 setDrawerLockMode() 什么都不做。

This would mean that by setting a gravity of "start|bottom", you're introducing an invalid gravity, which causes setDrawerLockMode() to do nothing.

这篇关于为什么我不能用重力布局锁定DrawerLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 21:15