开发者

Move view dynamically in relativelayout

开发者 https://www.devze.com 2023-03-21 02:16 出处:网络
I am doing an pageindex, and the current page should be highlighted with a arrow image (imageview). The index is a RelativeLayout with 25 textviews added to it:

I am doing an pageindex, and the current page should be highlighted with a arrow image (imageview).

The index is a RelativeLayout with 25 textviews added to it:

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);
    }

And when the page changes I 开发者_StackOverflowdo something like this:

        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();

Everything looks correct, I can place the arrow at arbitrary page at "start up", but the arrow wont move when page is changed. I have debuged and verified that the code is run and everything looks correct, but still the arrow is stuck in the first position. If I force delete and add a new imageview instead of updating the LayoutParams the arrow will disappear totally.


I have the same problem.. I also want to move my views around at run-time inside a reference layout. So if anyone ca help that would be awesome.

What i believe is happening above is that the arrow IS having its location changed, however it doesn't get updated to the screen. Correct me if I'm wrong, please, this is just my guess as I am too having the same problem.

-edit- After some messing around I've found what works for me is to remove first then add. rl.removeView(tv); rl.addView(tv,params);

-edit- also, u can save the params for the moving view into a unique variable so that way all ude have to do is change the margins... for example: instead of params, have it be its own name "arrowParams" then to move it ude just need: arrowParams.leftMargin = 2000+i; and so on...

0

精彩评论

暂无评论...
验证码 换一张
取 消