开发者

Only some Layout Params work

I am trying to align a custom View object above a开发者_C百科nd aligned_left of a RelativeLayout. My (snipped) code looks something like this:

int bored = board.getId(); //board is the RelativeLayout
Border border = new Border();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_LEFT, bored);
params.addRule(RelativeLayout.ABOVE, bored);
this.addView(border,params);

This gives me a Border object aligned_left with my RelativeLayout but NOT "ABOVE" (technically, it is above it, but it's at the top of the screen not aligned the way it's supposed to be). And, even stranger, when I do this:

int bored = board.getId();
Border border = new Border();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_LEFT, bored);
params.addRule(RelativeLayout.BELOW, bored); //<---The only difference
this.addView(border,params);

...it works perfectly. it is aligned left and below in the ways that are expected. Why is the ABOVE attribute giving me such a hard time?

Edit:

ABOVE is supposed to align the bottom edge of one view with the top edge of the target. Here is a screen shot of how my project is not working as intended.

Only some Layout Params work

The green bar at the top of the screen is the Border object I am trying to align. As you can see, its left edge is aligned with "board" however it is in the wrong place at the top of the screen.


Can you check if board and Border are not using more space? for example, by setting the background to a color to see their dimensions in the screen?

Maybe board, which you mention is a RelativeLayout is bigger of what you think.

I hope this helps


I fixed my problem by using an ImageView instead of my custom Border class (which was basically just a bitmap drawn to a canvas from a resource). My code now looks like this:

int bored = board.getId(); //board is the RelativeLayout
ImageView border = new ImageView(this);
border.setImageResource(R.drawable.border_tiki1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_LEFT, bored);
params.addRule(RelativeLayout.ABOVE, bored);
this.addView(border,params);

My best guess is that it was having trouble defining the ABOVE constant for my custom object. Thanks to everyone who took the time to look over this. Here's what my view looks like now:

Only some Layout Params work

From this anchor, I'll build the rest of the border out of other small pieces. Thanks again!


Without seeing the rest of the code, this is just a guess, but the order in which the objects are created makes a difference in how the objects are layed out. Especially where Relative Layouts are concerned.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜