GridLayout不能在整个屏幕上均匀地隔开孩子

时间:2016-05-11 12:18:48

标签: android xml android-linearlayout grid-layout relative

我有一个GridLayout,里面是6个LinearLayout子项。里面有按钮和其他东西。

在android Studio中它看起来很好,但是在实际设备(甚至是模拟器)上它不会均匀分布。这是正在发生的事情的图片。 enter image description here

<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>

3 个答案:

答案 0 :(得分:4)

这个问题是android:layout_columnWeightandroid:layout_rowWeight是Lollipop中的新功能,因此在此之前不支持。您可以使用GridLayoutapp:layout_columnWeight / app:layout_rowWeight中的support library来获取旧版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>

答案 1 :(得分:1)

您需要将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>

答案 2 :(得分:0)

<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"/>

scaleType

中尝试fitXYImageButton
相关问题