Setting button alignment programmatically

后端 未结 4 1260
轮回少年
轮回少年 2020-12-22 10:51

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

btn.Text=result.Rows[i]["Col1"].ToString()+"\\n"+result.Row         


        
相关标签:
4条回答
  • 2020-12-22 10:53

    Alignment of button content can be set with .setGravity(int)

    0 讨论(0)
  • 2020-12-22 11:04

    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;
    
    0 讨论(0)
  • 2020-12-22 11:04

    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:

    Button myButton = new Button(getActivity());
    myButton(Gravity.LEFT);
    
    0 讨论(0)
  • 2020-12-22 11:18
    Button myButton = new Button();
    myButton.setGravity(Gravity.RIGHT);   //LEFT
    

    Hope this help you. Worked for my (i was also strugling with this issue for some time)

    0 讨论(0)
提交回复
热议问题