ALIGN_BOTTOM in relation to the Parent class

Asked

Viewed 34 times

0

I need the menu to stay ALIGN_BOTTOM compared 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));

}

1 answer

1

According to the method documentation addRule. Use the ALIGN_BOTTOM means you want to line up a View in relation to the background of another View with the id specified as anchor.

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

Exchange the ALIGN_BOTTOM for ALIGN_PARENT_BOTTOM, take 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));

}
  • had already tried it, but it didn’t work.

  • @Igorronner, take a look at Hierarchy Viewer, in the DDMS or direct from the sdk, if the AppBar is large enough for him to align the menu into the background.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.