Setting Button Alignment Programmatically
Solution 1:
You can set the gravity of the button to customize how the text is aligned. This is exposed on the button by using the Gravity
property. From the docs:
Sets the horizontal alignment of the text and the vertical gravity that will be used when there is extra space in the TextView beyond what is required for the text itself.
The values you can assign are found in the GravityFlags enum. For example:
button.Gravity = GravityFlags.Center;
Solution 2:
Alignment of button content can be set with .setGravity(int)
Solution 3:
In the original question, I wonder if the source text contained some extra space characters? That would explain img1 and img2.
Anyway, my need to left-align the text in a button led me to this page, and here's the solution I ended up with, based on Alix Bloom's answer:
ButtonmyButton=newButton(getActivity());
myButton(Gravity.LEFT);
Solution 4:
ButtonmyButton=newButton();
myButton.setGravity(Gravity.RIGHT); //LEFT
Hope this help you. Worked for my (i was also strugling with this issue for some time)
Post a Comment for "Setting Button Alignment Programmatically"