Подтвердить что ты не робот

Как создать макет с 6 кнопками, такими как оконные плитки

Я пытаюсь создать макет с 6 кнопками, которые автоматически приспосабливаются к размеру экрана как плитки окна телефона. В коде я динамически создаю кнопку 6, 2 для линии, но кнопка должна соответствовать размеру экрана, заполняющего последний. как я могу продолжить?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up" />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
        />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
        />

</LinearLayout>

4b9b3361

Ответ 1

Я бы использовал вертикальный LinearLayout с тремя рядами того же веса, что и дети, причем каждая строка была горизонтальной LinearLayout, имеющей двух детей с одинаковыми весами, что позволит заполнить всю область. Для шести кнопок производительность не должна быть проблемой.

Если производительность является проблемой, вы можете сделать строки как RelativeLayout и использовать стойку для разделения пополам и поместить на нее двух детей.

Когда я говорю "стойка", я имею в виду это:

<View android:id="@+id/strut"
    android:layout_width="0dp"
    android:layout_height="0dp" 
    android:layout_centerHorizontal="true"/>

Update: Поскольку вы пытаетесь использовать LinearLayout s, вот как вы можете справиться с высотами и ширинами:

Родительский LinearLayout может иметь:

android:layout_width="match_parent"
android:layout_height="match_parent"

Три ребенка LinearLayout будут иметь:

android:layout_width="match_parent"
android:layout_height="0dip"

Button будет иметь:

android:layout_width="0dip"
android:layout_height="match_parent"

Как вы можете заметить, у нас есть 0dip для свойства, на которое применяется вес (либо на высоте, если родитель ориентирован по вертикали, либо ширина, если родитель ориентирован горизонтально), который нужно будет увеличивать, чтобы заполнить пространство.

Здесь полный XML (кнопки не включают drawables, поэтому не стесняйтесь добавлять свои):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

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

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

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

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

И результат: enter image description here

Ответ 2

Думаю, вам стоит взглянуть на GridView

Ответ 3

Использовать GridLayout! Это идеально подходит для этой ситуации.

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:layout_margin="0.5dp"
    android:columnCount="2"
    android:rowCount="3" >

    <Button
        android:id="@+id/b_1"
         />

    <Button
        android:id="@+id/b_2"
         />

    <Button
        android:id="@+id/b_3"
         />

    <Button
        android:id="@+id/b_4"
         />

    <Button
        android:id="@+id/b_5"
         />

    <Button
        android:id="@+id/b_6"
        />
</GridLayout>

Ответ 4

Я использую библиотеки Android .v7. Этот xml работал у меня, чтобы создать 2 столбца, трехстрочный макет, который заполняет весь экран:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/lib/android.support.v7.widget.GridLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    android:layout_margin="0.5dp"
    app:columnCount="2"
    app:rowCount="3"
    app:useDefaultMargins="true">

    <Button
        android:id="@+id/b_1"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_row="0"
        grid:layout_column="0"
        android:text="Hellooo"/>

    <Button
        android:id="@+id/b_2"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_row="0"
        grid:layout_column="1"
        android:text="Hellooo"/>

    <Button
        android:id="@+id/b_3"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_row="1"
        grid:layout_column="0"
        android:text="Hellooo"/>

    <Button
        android:id="@+id/b_4"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_row="1"
        grid:layout_column="1"
        android:text="Hellooo"/>

    <Button
        android:id="@+id/b_5"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_row="2"
        grid:layout_column="0"
        android:text="Hellooo"/>

    <Button
        android:id="@+id/b_6"
        grid:layout_columnWeight="1"
        grid:layout_rowWeight="1"
        grid:layout_row="2"
        grid:layout_column="1"
        android:text="Hellooo"/>

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