我正在做一個pageindex,並且當前頁面應該用箭頭圖像(imageview)突出顯示。在relativelayout中動態地移動視圖
該指數是25個textviews加入到它的RelativeLayout:
for (int i = 0; i < 25; i++) {
TextView tv = new TextView(this);
tv.setText(Integer.toString(i+1));
tv.setGravity(Gravity.CENTER);
int id = 2000+i;
tv.setId(id);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
if(i==0)
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
else
params.addRule(RelativeLayout.RIGHT_OF, prevViewId);
prevViewId = id;
rl.addView(tv,params);
}
而當頁面改變我做這樣的事情:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BOTTOM, 2000+i);
params.addRule(RelativeLayout.ALIGN_RIGHT, 2000+i);
params.addRule(RelativeLayout.ALIGN_LEFT, 2000+i);
arrowImageView.setLayoutParams(params);
rl.requestLayout();
rl.invalidate();
一切看起來是正確的,我可以把箭頭在「啓動」的任意頁面上,但是頁面改變時箭頭不會移動。我已經發現並驗證了代碼已經運行,並且所有內容都看起來正確,但仍然是箭頭停留在第一個位置。如果我強制刪除並添加新的imageview,而不是更新LayoutParams,則箭頭將完全消失。