问题描述
我正在动态创建大约 50 个按钮.
文本设置如下:
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();
其中 result
是 DataTable
&btn
是按钮的对象.
where result
is DataTable
& btn
is object for button.
现在的问题是一些按钮没有正确显示.
Now the problem is some of the buttons are not getting displayed appropriately.
参考下面的截图,
在 img1 中 - 在第一行之后显示了一个不必要的空行.
在 img2 中 - 文本未居中对齐.
在 img3 中 - TATAMOTORS 没有显示在单行中,即使按钮两侧都有空格.
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.
感谢任何帮助...
推荐答案
您可以设置按钮的重力来自定义文本的对齐方式.这是通过使用 Gravity
属性在按钮上公开的.来自文档:
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:
设置文本的水平对齐方式以及当 TextView 中存在超出文本本身所需空间的额外空间时将使用的垂直重力.
您可以分配的值可在 GravityFlags 枚举中找到一>.例如:
The values you can assign are found in the GravityFlags enum. For example:
button.Gravity = GravityFlags.Center;
这篇关于以编程方式设置按钮对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!