Reputation: 2143
layout1 is composed of status bar at the top and toolbar at the bottom of screen. And it is defined in xml. I want to place layout2 between layout1's status bar and toolbar programmatically.
setContentView(R.layout.layout1);
layout2 = new MyLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = 0;
params.addRule(RelativeLayout.BELOW, R.id.statusbar); // this does not work.
addContentView(layout2, params);
This code puts layout2 at the top of screen so it hides layout1's status bar. How can I place layout2 at the location I want? Is there other way not using addContentView?
Upvotes: 0
Views: 611
Reputation: 51
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
I tried this work.
Upvotes: 2
Reputation: 2097
Set gravity in linear layout programmatically.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
Upvotes: 0