android 控件設置GONE,其實父佈局就已經重繪了,在開發一個功能時,遇到一個問題,就是佈局重繪(GONE,QQ消息狀態欄彈框等)導致某個控制設置的座標無效而回到原點,先上代碼
private void updateVirtualCursorXY(int l, int t, int r, int b) {
try {
// pc_control_cursor.layout(l, t, r, b);
// pc_control_cursor.postInvalidate();
moveView(pc_control_cursor, l, t,r, b, pc_control_cursor.getWidth(), pc_control_cursor.getHeight());
} catch (Exception e) {
e.printStackTrace();
}
}
private void moveView(View v, int left, int top, int right, int bottom, int width, int height) {
// RelativeLayout.LayoutParams params = null;
// params = new RelativeLayout.LayoutParams(width, height);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
params.width = (int)(CURSOR_WIDTH * Utils.getScreenDensity());
params.height = (int)(CURSOR_HEIGHT * Utils.getScreenDensity());
params.leftMargin = left;
params.topMargin = top;
params.rightMargin = viewWidth - right;
params.bottomMargin = viewHeight - bottom;
v.setLayoutParams(params);
}
函數updateVirtualCursorXY可以看到是爲了更新控件的座標,最初用的是被屏蔽的兩行代碼用來更新控件座標,後來發現只要佈局重繪就會導致控件回到原點,網上查了資料發現當view的位置發生改變時,也要相應改變其layoutparams,否則父佈局重繪時,由於view的layoutparams沒發生改變導致重繪的時候會回到原點,所以只要改變其params就好了。