Hi I am having troubles trying to get this Relative Layout placed below another Relative Layout. In my Activity I have a NavBar (RelativeLayout) that is aligned to the top of the activity. I would like to place my TitleBar (RelativeLayout) below this programmatically.
Here is my onCreate method where I allocate both the NavBar and TitleBar and add them to my Activity. The NavBar is correctly aligned at the top of the activity, however, the TitleBar is aligned with the top of the activity as well. I'd like the top of the TitleBar to align with the bottom of the NavBar so it is placed below the NavBar.
super.onCreate(bundle);
Context context = getApplicationContext();
RelativeLayout.LayoutParams params;
//navbar
this.navBar = new BINavBar(context);
relativeLayout.addView(this.navBar);
params = (LayoutParams)this.navBar.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
this.navBar.setLayoutParams(params);
//titleBar
this.titleBar = new BITitleBar(context);
relativeLayout.addView(this.titleBar);
params = (LayoutParams)this.titleBar.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.BINavBar_layout);
this.titleBar.setLayoutParams(params);
I don't think you are actually setting the id of the nav bar. Try the following:
super.onCreate(bundle);
Context context = getApplicationContext();
RelativeLayout.LayoutParams params;
//navbar
this.navBar = new BINavBar(context);
relativeLayout.addView(this.navBar);
params = (LayoutParams)this.navBar.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
this.navBar.setLayoutParams(params);
this.navBar.setId(R.id.BINavBar_layout); // this is the change
//titleBar
this.titleBar = new BITitleBar(context);
relativeLayout.addView(this.titleBar);
params = (LayoutParams)this.titleBar.getLayoutParams();
params.addRule(RelativeLayout.BELOW, this.navBar.getId()); // this should use navBar's id
this.titleBar.setLayoutParams(params);
Note that you may need to set the id yourself. Are you sure that R.id.BINavBar_layout
exists? If not, try using the following function: View.generateViewId()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments