1

我有一个FrameLayout(在 a 内)到/从它通过类RelativeLayout添加/删除。My最初与其父级左对齐,有时通过该方法水平偏移。FragmentsFragmentTransactionFrameLayoutFrameLayout.offsetLeftAndRight(int offset)

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

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

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

4

1 回答 1

0

通过跟踪偏移位置并onLayout(boolean changed, int left, int top, int right, int bottom)在调用此方法时覆盖我的 View 的方法以适当地偏移我的 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);
  }
}
于 2012-07-27T13:32:40.077 回答