0
我有一個帶有TextView子項的FrameLayout,我想在運行時更改TextViews的位置(在onSensorChanged()中,因此每秒幾次)。沒有setPosition(...),那麼我該怎麼做?在運行時將子視圖位置更新到父視圖
感謝
我有一個帶有TextView子項的FrameLayout,我想在運行時更改TextViews的位置(在onSensorChanged()中,因此每秒幾次)。沒有setPosition(...),那麼我該怎麼做?在運行時將子視圖位置更新到父視圖
感謝
所有你需要的是View.layout(int,int,int,int)
方法。
Reference說:
public void layout (int l, int t, int r, int b) Since: API Level 1 Assign a size and position to a view and all of its descendants This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them. This is typically done using the child measurements that were stored in the measure pass().
FrameLayout
並不好做這樣的事情,與RelativeLayout
你可以做到以下幾點:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)child.getLayoutParams();
layoutParams.leftMargin = x;
layoutParams.topMargin = y;
child.setLayoutParams(layoutParams);
在onSensorChanged我想'view.layout(X,Y ,x + view.getWidth(),y + view.getHeight());'對於每個視圖,其中x和y是新的座標,並且視圖甚至不顯示... – jul
注意x和y相對於家長。確保您的視圖寬度和高度不爲零。 –
他們不是......佈局被調用時,l,t,r和b是正確的。但沒有顯示意見。我試圖使無效()父母,它不會改變任何東西... – jul