It seems I'm not out of the woods just yet. I seem to be having another strange problem with gravity - vertically this time. LEFT, RIGHT & CENTER (aligning a normal and wider button in a vertical layout so that they are both right-aligned) seem to be no problem.

Given the following code sample:

---
LinearLayout rootContainer = new LinearLayout(this);
rootContainer.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams constraints = null ;
int normalGravity = Gravity.TOP ;
int tallerGravity = Gravity.BOTTOM ;

   LinearLayout nestedContainer = new LinearLayout(this);
   nestedContainer.setOrientation(LinearLayout.HORIZONTAL);
   nestedContainer.setPadding(0, 0, 0, 0);
nestedContainer.setBackgroundColor(Color.YELLOW); // Confirm both buttons in the container.

   Button normalButton = new Button(this);
   normalButton.setText("Normal");
   constraints = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
           LayoutParams.WRAP_CONTENT, 0.0f);
   constraints.gravity = normalGravity ;
   nestedContainer.addView(normalButton, constraints);

   Button tallerButton = new Button(this);
   tallerButton.setText("Much Taller");
   constraints = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
           80, // Deliberately taller than required.
           0.0f);
   constraints.gravity = tallerGravity ;
   nestedContainer.addView(tallerButton, constraints);

constraints = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
       100, // Deliberately rather tall.
       0.0f);
rootContainer.addView(nestedContainer, constraints);

setContentView(rootContainer);

---

If you debug this, the normal button aligns to the top and the taller button to the bottom, as expected. (There's a padding issue - it adds padding around the taller button when it should not, but I can live with that as it's small.)

If you try changing both normalGravity and tallerGravity to both TOP or both BOTTOM, what happens is that the buttons seem to line up with each other centrally, and then the *row* is aligned to top or bottom, instead of each control aligning individually. This, to my mind, breaks the contract of having individual gravity for each control via its LayoutParams.

Thoughts?

I swear I'm not trying to break this stuff!


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to