来自支持库的GridLayout在API 16-17上无法正常工作

时间:2015-07-13 07:12:17

标签: android android-support-library android-gridlayout

我需要像计算器键盘一样创建smth。 GridLayout非常适合它(仅适用于它:))。所以,我使用支持库中的一个(compile 'com.android.support:gridlayout-v7:22.2.0'。这是我的xml:

<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:columnCount="4"
        app:rowCount="4"
        android:layout_gravity="bottom"
        android:layout_weight="4"
        app:alignmentMode="alignBounds"
        app:rowOrderPreserved="false"
        app:orientation="horizontal">

    <Button android:text="7"
            android:id="@+id/button7"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="8"
            android:id="@+id/button8"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="9"
            android:id="@+id/button9"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="←"
            android:id="@+id/button_backspace"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>

    <Button android:text="4"
            android:id="@+id/button4"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="5"
            android:id="@+id/button5"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="6"
            android:id="@+id/button6"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="@string/clean"
            android:id="@+id/button_clear"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>

    <Button android:text="1"
            android:id="@+id/button1"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="2"
            android:id="@+id/button2"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="3"
            android:id="@+id/button3"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button
            android:id="@+id/button_clear_all"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:layout_gravity="fill_vertical"
            android:text="@string/clean_all"
            app:layout_rowSpan="2"
            android:background="@color/calculator_button_separator_color"
            android:textColor="@drawable/light_button_text_color"
            style="@style/CalculatorButton"/>

    <Button
            android:id="@+id/button0"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:layout_gravity="fill_horizontal"
            android:text="0"
            app:layout_columnSpan="2"
            style="@style/CalculatorButton"/>
    <Button android:text="."
            android:id="@+id/button_dot"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>

</android.support.v7.widget.GridLayout>

在Android 4.3-4.4上一切正常:

但Android 4.1-4.2存在问题:

所以,我无法理解我的坏处。

4 个答案:

答案 0 :(得分:2)

嗯,这是支持库中的错误。我用编译'com.android.support:gridlayout-v7:22.0.+'替换了编译'com.android.support:gridlayout-v7:22.2.0',用代码制作了一些魔法,在4.1和4.3上检查过,现在它是工作!

<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:columnCount="4"
        app:rowCount="4"
        android:layout_gravity="bottom"
        android:layout_weight="4"
        app:alignmentMode="alignBounds"
        app:orientation="horizontal">

    <Button android:text="7"
            android:id="@+id/button7"
            app:layout_gravity="fill"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="8"
            android:id="@+id/button8"
            app:layout_gravity="fill"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="9"
            android:id="@+id/button9"
            app:layout_gravity="fill"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="←"
            android:id="@+id/button_backspace"
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>

    <Button android:text="4"
            android:id="@+id/button4"
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="5"
            android:id="@+id/button5"
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="6"
            android:id="@+id/button6"
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="@string/clean"
            app:layout_gravity="fill"
            android:id="@+id/button_clear"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>

    <Button android:text="1"
            android:id="@+id/button1"
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="2"
            android:id="@+id/button2"
            app:layout_columnWeight="1"
            app:layout_gravity="fill"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
    <Button android:text="3"
            android:id="@+id/button3"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:layout_gravity="fill"
            style="@style/CalculatorButton"/>
    <Button
            android:id="@+id/button_clear_all"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:layout_gravity="fill"
            android:text="@string/clean_all"
            app:layout_rowSpan="2"
            android:background="@color/calculator_button_separator_color"
            android:textColor="@drawable/light_button_text_color"
            style="@style/CalculatorButton"/>

    <Button
            android:id="@+id/button0"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:layout_gravity="fill"
            android:text="0"
            app:layout_columnSpan="2"
            style="@style/CalculatorButton"/>
    <Button android:text="."
            android:id="@+id/button_dot"
            app:layout_gravity="fill"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            style="@style/CalculatorButton"/>
</android.support.v7.widget.GridLayout>

答案 1 :(得分:1)

我刚刚在我的项目中解决了这个问题。为所有按钮添加android:layout_width="0dp"android:layout_height="0dp"可以解决问题。以下是我的布局中的部分:

<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill"
    android:padding="16dp"
    app:columnCount="4"
    app:rowCount="5"
    app:orientation="horizontal">

    <Button
        android:text="Test"
        app:layout_gravity="fill"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"/>

    ...

</android.support.v7.widget.GridLayout>

答案 2 :(得分:0)

compile 'com.android.support:design:22.2.0'

或尝试创建一个Adapter并通过该

将数据添加到GridView

答案 3 :(得分:0)

你在LogCat上有任何错误吗?

如果您没有得到任何内容,请尝试导入gridlayout-v7

compile 'com.android.support:gridlayout-v7:19.0.0'

同时确保您已获得8 + minSdkVersion

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />

您是否尝试将android.support.v7.widget.GridLayout替换为GridLayout