Android Button in Gridlayout don't have same m

2019-07-25 01:41发布

问题:

I got a GridLayout, and there will be two or more buttons in the layout, and all the buttons must with same width. My problem is that when the buttons having different line number of text, the marginTop of button will different too. How can I fix this? Thanks.

<GridLayout
    android:id="@+id/grid_buddies"
    android:columnCount="4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:text="TEST"/>
    <Button
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:text="TESTDDFF"/>
</GridLayout>

Here is the picture:

回答1:

Use android:layout_gravity="fill_vertical" attribute in your Button with text TEST

<GridLayout
    android:id="@+id/grid_buddies"
    android:columnCount="4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
       android:layout_width="60dp"
       android:layout_height="match_parent"
       android:text="TEST"
       android:layout_gravity="fill_vertical"/>
    <Button
       android:layout_width="60dp"
       android:layout_height="wrap_content"
       android:text="TESTDDFF"/>
</GridLayout>