为什么我不能用布局重力锁定 DrawerLayout [英] Why i can't lock DrawerLayout with layout gravity

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

问题描述

我使用 DrawerLayout,最近我想改变 drawerLayout 中 listView 的重力.但是在我将 listView 的重力从 android:layout_gravity="start" 更改为 android:layout_gravity="start|bottom" 后,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.LEFTGravity.RIGHTGravityCompat.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.

(强调我的):

抽屉的定位和布局是使用android:layout_gravity 属性对应于哪个子视图您希望抽屉出现的视图的一侧:左侧右侧.(或者在支持布局方向的平台版本上开始/结束.)

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.)

查看源代码

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
  }
}

方法本身只检查重力是LEFT 还是RIGHT(但使用GravityCompat 方法,所以STARTcode> 和 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).

这意味着通过设置 "start|bottom" 的重力,您会引入无效的重力,这会导致 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆