GridLayout 在整个屏幕上的间距 children 不均匀
GridLayout not evenly spacing children throughout screen
我有一个 GridLayout,里面有 6 个 LinearLayout children。里面有按钮和其他东西。
在 Android Studio 中它看起来不错,但是在实际设备(甚至模拟器)上它并不 space 均匀。这是正在发生的事情的图片。
<RelativeLayout <!--main layout stuff-->
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="4"
android:columnCount="2">
<!--
there are six of these inside the grid layout
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button"
android:src="@drawable/button"
android:layout_gravity="center"/>
</LinearLayout>
</GridLayout>
</RelativeLayout>
您需要将 LinearLayout
的高度和宽度设置为 0dp
以匹配父布局
看看这个
<RelativeLayout <!--main layout stuff-->
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="4"
android:columnCount="2">
<!--
there are six of these inside the grid layout
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button"
android:src="@drawable/button"
android:layout_gravity="center"/>
</LinearLayout>
</GridLayout>
</RelativeLayout>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button"
android:src="@drawable/drawer_bg"
android:layout_gravity="center"
android:scaleType="fitXY"/>
在你的 ImageButton
中尝试 scaleType
到 fitXY
此问题是 android:layout_columnWeight
和 android:layout_rowWeight
在 Lollipop 中是新的,因此在此之前不受支持。您可以使用 GridLayout from the support library 和 app:layout_columnWeight
/app:layout_rowWeight
为较旧的 android 设备获取这些功能。
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rowCount="4"
app:columnCount="2">
<!--
there are six of these inside the grid layout
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button"
android:src="@drawable/button"
android:layout_gravity="center"/>
</LinearLayout>
</GridLayout>
我有一个 GridLayout,里面有 6 个 LinearLayout children。里面有按钮和其他东西。
在 Android Studio 中它看起来不错,但是在实际设备(甚至模拟器)上它并不 space 均匀。这是正在发生的事情的图片。
<RelativeLayout <!--main layout stuff-->
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="4"
android:columnCount="2">
<!--
there are six of these inside the grid layout
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button"
android:src="@drawable/button"
android:layout_gravity="center"/>
</LinearLayout>
</GridLayout>
</RelativeLayout>
您需要将 LinearLayout
的高度和宽度设置为 0dp
以匹配父布局
看看这个
<RelativeLayout <!--main layout stuff-->
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="4"
android:columnCount="2">
<!--
there are six of these inside the grid layout
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button"
android:src="@drawable/button"
android:layout_gravity="center"/>
</LinearLayout>
</GridLayout>
</RelativeLayout>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button"
android:src="@drawable/drawer_bg"
android:layout_gravity="center"
android:scaleType="fitXY"/>
在你的 ImageButton
scaleType
到 fitXY
此问题是 android:layout_columnWeight
和 android:layout_rowWeight
在 Lollipop 中是新的,因此在此之前不受支持。您可以使用 GridLayout from the support library 和 app:layout_columnWeight
/app:layout_rowWeight
为较旧的 android 设备获取这些功能。
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rowCount="4"
app:columnCount="2">
<!--
there are six of these inside the grid layout
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/button"
android:src="@drawable/button"
android:layout_gravity="center"/>
</LinearLayout>
</GridLayout>