LayoutParams将视图放置在奇怪的位置

时间:2013-04-23 15:18:10

标签: android android-layout relativelayout

我目前在我的应用程序中有一个RelativeLayout,我动态移动,调整大小和隐藏。移动一次后,布局会重新定位到一个奇怪的位置。

这是我正在使用的布局。

<RelativeLayout ...>
    <RelativeLayout android:id="@+id/moveable_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:visibility="gone" />
</RelativeLayout>

以下是我用来重新定位的代码

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, newHeight);
m_movingFrame.setEnabled(true);
m_movingFrame.setLayoutParams(layoutParams);
m_movingFrame.setX(newX);
m_movingFrame.setY(newY);
m_movingFrame.requestLayout();
m_movingFrame.bringToFront();
m_movingFrame.setVisibility(View.VISIBLE);

以下是用于隐藏布局的代码

m_movingFrame.setEnabled(false);
m_movingFrame.setVisibility(View.GONE);

我第一次重新定位它时,它会移动到正确的位置,但每次后续重定位都会将其移动到错误的位置。任何人都可以告诉我为什么它表现得如此不规律?

提前致谢!

1 个答案:

答案 0 :(得分:0)

首先获取现有布局的布局,然后进行修改。

 RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) m_movingFrame.getLayoutParams();

...

m_movingFrame.setLayoutParams(layoutParams);
相关问题