我需要创建类似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上一切正常:
但安卓4.1-4.2也有问题:
所以,我不知道我的坏处在哪里。

最佳答案

嗯,这真的是支持库中的bug。我用compile“com.android.support:gridlayout-v7:22.2.0”替换了compile“com.android.support:gridlayout-v7:22.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>

关于android - 支持库中的GridLayout在API 16-17上无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31377357/

10-11 22:51