Wrapped Button Text Adds Extra Margin

Nathan Sokalski 4,121 Reputation points
2022-06-11T23:41:06.58+00:00

I have a GridLayout of Buttons, one of which has text that wraps to 2 lines. However, this wrapping adds extra space above the Button, causing the Buttons to no longer be vertically aligned. If I place the Button with wrapped text inside a FrameLayout, it looks fine. However, having 1 Button inside a FrameLayout while the others are not makes the code much less efficient, and placing all the Buttons inside a FrameLayout would add a lot of extra elements, possibly hurting performance. Here is the xml for the Button with & without the FrameLayout:

<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_row="2">  
	<Button style="@style/EntryButton" android:textSize="18dp" android:text="Two Lines"/>  
</FrameLayout>  

<Button style="@style/EntryButton" android:layout_column="0" android:layout_row="2" android:textSize="18dp" android:text="Two Lines"/>  

And here are screenshots (from the Visual Studio 2022 Designer, but the same thing happens when debugging):

210515-screenshot-2022-06-11-171553.png

210545-screenshot-2022-06-11-171701.png

Why is the text wrapping adding extra space only when not inside the FrameLayout, and is there any easy way to avoid this?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,386 Reputation points Microsoft Vendor
    2022-06-13T09:34:47.077+00:00

    Hello,​

    is there any easy way to avoid this?

    Please add android:layout_marginBottom= directly for your button.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.