ALIGN_BOTTOM in relation to the parent class

0

I need the menu to stay ALIGN_BOTTOM in relation to my AppBar, but the code below does not work.

public class AppBar extends RelativeLayout { 

 public AppBar(Context context) {

    RelativeLayout menu = new RelativeLayout(context);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, this.getId());
    menu.setLayoutParams(layoutParams);
    this.addView(menu);

    this.setBackgroundResource(PKImageLoader.getResourceId(context,
            backgroundName));

}
    
asked by anonymous 08.08.2014 / 19:31

1 answer

1

According to the method documentation addRule . Using ALIGN_BOTTOM means that you want to align a View against the background of another View with id specified as anchor .

However, making this rule with id of AppBar (parent) has no effect, because it probably does not find the element and ignores, because id needs to be another child RelativeLayout .

Change ALIGN_BOTTOM to ALIGN_PARENT_BOTTOM , get the second parameter and check the result.

public AppBar(Context context) {

    RelativeLayout menu = new RelativeLayout(context);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    menu.setLayoutParams(layoutParams);

    this.addView(menu);

    this.setBackgroundResource(PKImageLoader.getResourceId(context, backgroundName));

}
    
08.08.2014 / 19:58