GridLayout with Equal Column Widths: wrap_content vs match_parent

Nathan Sokalski 3,976 Reputation points
2021-11-09T05:40:45.28+00:00

I have a GridLayout that contains 2 columns of buttons of which I want to be the same width. I have achieved this with no problem using layout_columnWeight="1" and layout_width="0" in the buttons. However, this only seems to work if I set the GridLayout's layout_columnWidth to match_parent or a fixed width, if I use a value of wrap_content it displays nothing. Doing this makes everything much bigger than necessary & desired. What can I do to minimize the width of the GridLayout while still keeping the columns equal width?

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

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,846 Reputation points
    2021-11-10T05:50:22.047+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    no problem using layout_columnWeight="1" and layout_width="0" in the buttons. ... if I use a value of wrap_content it displays nothing.

    When setting layout_width to wrap_content, the gridLayout will caculate the width of the content. Because the layout_width of the two buttons' was set to 0, the width of the gridLayout will also be 0.

    To fix this, please change the layout_width of the two buttons to wrap_content. This will also work when setting gridLayout's widht to match_parent.

       <GridLayout  
           android:padding="2dp"  
           android:rowCount="1"  
           android:background="#0f0"  
           android:columnCount="2"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content">  
         
           <Button   
               android:text="Button1"  
               android:layout_height="wrap_content"  
               android:layout_width="wrap_content"  
               android:layout_row="0"  
               android:layout_columnWeight="1"  
               android:layout_column="0"/>  
         
           <Button   
               android:text="Button2"  
               android:layout_height="wrap_content"  
               android:layout_width="wrap_content"  
               android:layout_row="0"  
               android:layout_columnWeight="1"  
               android:layout_column="1"/>  
         
       </GridLayout>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, 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.

    No comments