There is a code:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); ......... final ToggleButton myLightButton = new ToggleButton(this); myLightButton.setText("BTN1"); RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); myLightButton.setLayoutParams(lp2); addContentView(myLightButton, lp2); 

The button itself is displayed, but in the upper left corner, and in the lower right does not want

I tried this:

  ToggleButton myLightButton = new ToggleButton(this); myLightButton.setText("LightButton"); RelativeLayout.LayoutParams buttonParam1 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); buttonParam1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); buttonParam1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); myLightButton.setLayoutParams(buttonParam1); RelativeLayout relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); relativeLayout.setLayoutParams(param); relativeLayout.addView(myLightButton); addContentView(myLightButton, param); //setContentView(relativeLayout); 

But then the button is not displayed, or a new layou is created with the button as it should, but completely closes the graphics that I have displayed in MyView ()

  • why not through hml? - Gorets


1 answer 1

Try passing another LayoutParams object to LayoutParams .

  • Not really understood. Those. to create another LayoutParams object? - tolik777 '11
  • Exactly! - DroidAlex