Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

android - Setting button alignment programmatically

I'm creating some 50 button dynamically.
Text is getting set as followed:

btn.Text=result.Rows[i]["Col1"].ToString()+"
"+result.Rows[i]["Col2"].ToString()
+"
"+result.Rows[i]["Col3"].ToString();

where result is DataTable & btn is object for button.

Now the problem is some of the buttons are not getting displayed appropriately.

Referring to screenshot below,

in img1 - An unnecessary blank line is getting displayed after the first row.
in img2 - Text is not center aligned.
in img3 - TATAMOTORS is not getting displayed in single line even though there is a space on either side of t he button.

Please note that I'm not setting padding which can be the reason for this.

Any idea how to solve this?
Also, how alignment of text of a button can be set programmatically?

I know that this is not the Best of the question, but after spending hours on it, I'm unable to crack it.

Any help appreciated...

Screen shot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...