在 xml 中实现没有嵌套权重和固定大小的键盘布局

Implement keypad layout without nested weights and fixated size in xml

我的布局有很多嵌套权重,这不利于性能。 如何制作没有重量的计算器键盘,但保存按钮的大小而不给它们固定 "sp" 或 "dp" 大小,无论哪种分辨率,键盘都必须缩放屏幕现在

我在这里找到了解决方案CommonsWare answer and here Che Jame answer

但为什么他们在两个按钮上都说 "Use android:layout_weight="1" Eclipse 说我必须得到砝码的簧片... 还有其他解决方案吗?或者在 android 平台上是不可能的? 这是我使用嵌套权重的方式,代码在

下面

我想保存这个视图,但是没有嵌套的权重,按钮的大小必须相同,大于 "wrap content" 参数

感谢任何建议。

 <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0sp"
                        android:layout_weight="1"
                        android:orientation="vertical" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="0sp"
                            android:layout_weight="1"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/cashcount_form_button1"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_1"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button2"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_2"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button3"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_3"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="0sp"
                            android:layout_weight="1"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/cashcount_form_button4"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_4"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button5"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_5"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button6"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_6"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="0sp"
                            android:layout_weight="1"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/cashcount_form_button7"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_7"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button8"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_8"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button9"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_9"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="0sp"
                            android:layout_weight="1"
                            android:orientation="horizontal" >

                            <Button
                                android:id="@+id/cashcount_form_comma_button"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_comma"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />

                            <Button
                                android:id="@+id/cashcount_form_button0"
                                android:layout_width="0dp"
                                android:layout_height="fill_parent"
                                android:layout_weight="1"
                                android:text="@string/cashcount_key_0"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/white"
                                android:textSize="35sp"
                                android:textStyle="bold" />
                        <Button
                            android:id="@+id/cashcount_form_delete_button"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:drawableStart="@drawable/ic_action_back_sign_image_view"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textColor="@color/white"
                            android:textSize="35sp"
                            android:textStyle="bold" />
                    </LinearLayout>

                </LinearLayout>

因为 suggested by Android LinearLayouts should be replaced by RelativeLayout or GridLayout. RelativeLayout will suffice for most situations but GridLayout 在像这种情况下可以派上用场。您可以编写类似于以下代码的内容来实现您想要的:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:columnCount="4"
    android:orientation="horizontal" >

    <Button
        android:layout_column="3"
        android:text="/" />

    <Button android:text="1" />

    <Button android:text="2" />

    <Button android:text="3" />

    <Button android:text="*" />

    <Button android:text="4" />

    <Button android:text="5" />

    <Button android:text="6" />

    <Button android:text="-" />

    <Button android:text="7" />

    <Button android:text="8" />

    <Button android:text="9" />

    <Button
        android:layout_gravity="fill"
        android:layout_rowSpan="3"
        android:text="+" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="0" />

    <Button android:text="00" />

    <Button
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="=" />

</GridLayout>