XML
<androidx.gridlayout.widget.GridLayout
android:id="@+id/manage_grid"
app:orientation="horizontal"
app:columnCount="3"
android:layout_marginEnd="-10dp" //因为子元素都设置了marginEnd10 所以最后容器的宽度要超过屏幕后的10dp才可看见均分的效果
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:background="@color/cardview_dark_background"
android:textColor="@color/cardview_light_background"
android:text="1"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_columnWeight="1"
android:layout_height="wrap_content"
android:padding="15dp" />
<TextView
android:background="@color/cardview_dark_background"
android:textColor="@color/cardview_light_background"
android:text="2"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_columnWeight="1"
android:layout_height="wrap_content"
android:padding="15dp" />
<TextView
android:background="@color/cardview_dark_background"
android:textColor="@color/cardview_light_background"
android:text="3"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_columnWeight="1"
android:layout_height="wrap_content"
android:padding="15dp" />
<TextView
android:background="@color/cardview_dark_background"
android:textColor="@color/cardview_light_background"
android:text="4"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_columnWeight="1"
android:layout_height="wrap_content"
android:padding="15dp" />
</androidx.gridlayout.widget.GridLayout>
Essentially what it's doing is setting a margin on each item (10dp) to space them inside the GridLayout and then it's shifting the GridLayout by using a negative margin (-10dp) to compensate for the additional width. Giving the following result;