如何设置列宽度相对于GridLayout宽度?



我想将列宽度设置为其父(GridLayout)宽度的四分之一,即使该行中只有一列。

<GridLayout
android:id="@+id/widget_place"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_marginVertical="10dp"
android:orientation="horizontal"
android:columnCount="3"
android:columnOrderPreserved="false"
>
<Button
android:id="@+id/btn"
android:text="card1"
android:layout_columnSpan="1"
android:layout_columnWeight="4"
android:layout_gravity="clip_horizontal"
/>
</GridLayout>

你可以使用android:layout_width属性将GridLayout中列的宽度设置为父列宽度的四分之一,并将其设置为"0dp"并且android:layout_weight属性设置为0.25。这告诉该列占用父GridLayout中剩余空间的25%。

<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Column 1" />
</GridLayout>

这将使列宽度为父列宽度的1/4,即使该行中只有一列。

最新更新