Реализовать макет клавиатуры без вложенных весов и фиксированного размера в xml

У меня есть макет с множеством вложенных весов, что плохо для производительности. Как можно сделать, например, клавиатуру калькулятора без утяжелителей, но сохраняя размер кнопок, не задавая им фиксированный размер "sp" или "dp", клавиатура должна масштабироваться независимо от того, какое разрешение на экране сейчас

Я нашел решение здесь ответ CommonsWare и здесь Che Jame answer

но почему они говорят: "Используйте android: layout_weight =" 1 "на обеих кнопках" Eclipse, говоря, что я должен получить язычок весов ... Есть ли другие решения? или это невозможно в платформе android? Вот как у меня это с вложенными весами, а код ниже

Я хочу сохранить это представление, НО без вложенных весов, и кнопки должны быть того же размера, больше, чем параметр «обернуть содержимое».

Спасибо за любой совет.

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

person android.dk    schedule 15.01.2015    source источник


Ответы (1)


Как предлагается Android, LinearLayouts следует заменить на RelativeLayout или GridLayout. RelativeLayout будет достаточно для большинства ситуаций, но 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>
person Varun Singh    schedule 15.01.2015