【问题标题】:FrameLayout which is offset re-positions itself when a Fragment is added to it?当将 Fragment 添加到其中时,偏移量的 FrameLayout 会重新定位自己吗?
【发布时间】:2012-07-03 18:39:16
【问题描述】:

我有一个FrameLayout(在RelativeLayout 内),我通过FragmentTransaction 类添加/删除Fragments。我的FrameLayout 最初与其父对象左对齐,有时通过FrameLayout.offsetLeftAndRight(int offset) 方法水平偏移。

我发现当我的FrameLayout 偏移并且我提交片段事务以向其添加Fragment(即FragmentTransaction.add(R.id.myFrameLayout, myNewFragment).commit())时,我的FrameLayout 跳回其原始位置,忽略其偏移.

任何人都知道为什么会发生这种情况,以及当我向其添加Fragment 时如何避免我的FrameLayout 重新定位?

注意:我使用的是offsetLeftAndRight(int offset) 方法而不是setTranslationX(float translationX) 或其他方法,因为我需要我正在做的工作来完成Honeycomb 之前的工作。

【问题讨论】:

    标签: android android-layout android-fragments android-view android-fragmentactivity


    【解决方案1】:

    通过跟踪偏移位置并覆盖我的 View 的 onLayout(boolean changed, int left, int top, int right, int bottom) 方法以在调用此方法时适当地偏移我的 View 来解决,如下所示:

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
      super.onLayout(changed, left, top, right, bottom);
    
      if (changed)
      {
        offsetLeftAndRight(myHorizontalOffset);
        offsetTopAndBottom(myVerticalOffset);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多