【问题标题】:Dynamically change SlidingDrawer content width动态改变 SlidingDrawer 内容宽度
【发布时间】:2013-09-11 00:56:15
【问题描述】:

我目前正在扩展 SlidingDrawer 类,并希望调整内容的宽度,但无法使其正常工作。现在我正在设置整个视图的宽度(句柄和内容),它用于调整大小的目的,但是当我移动句柄时它会在一瞬间跳转到新的大小,然后返回到句柄时也引入了视觉故障位置。我认为问题出在基本 SlidingDrawer 中发生的 onMeasure() 或 onLayout() 调用导致内容区域无法调整大小但不完全确定。

我正在使用getLayoutParams().width = newWidth; 调整整个视图的大小,但我想使用mContent.getLayoutParams().width = newWidth; 之类的东西。

onMeasure() 的源代码是 here 和 onLayout() here。

任何关于为什么内容区域无法调整大小的见解都会很棒。谢谢!

【问题讨论】:

    标签: android slidingdrawer


    【解决方案1】:

    所以我终于弄清楚是否有其他人对此有疑问。基本上,当您想要调整布局大小时,您需要 measure() 大小更改后的布局。如果没有 offsetLeftAndRight() 调用,句柄将在一瞬间“跳转”到新大小,因此设置偏移量可以消除“跳转”。

    我所做的简化版本基本上是:

    public void resize() {
       int previousPosition = mHandle.getLeft();
    
       //Set the new size of the content area
       mContent.getLayoutParams().width = width;
    
       //Measure the newly sized content area and adjust the layout
       mContent.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getBottom() - getTop(), MeasureSpec.EXACTLY));
       mContent.layout(handleWidth + mTopOffset, 0, mTopOffset + handleWidth + content.getMeasuredWidth(), content.getMeasuredHeight());
    
       /* Remeasure any other views that were resized also here */
    
       //Not required but helps position the handle correctly
       mHandle.offsetLeftAndRight(previousPosition);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      相关资源
      最近更新 更多