сделать это способом GridLayout в Android

Привет у меня есть этот экран

введите здесь описание изображения

что я сделал по этому коду.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.test.project"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="1" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="2" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="3" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="4" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="5" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="6" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="7" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="8" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="9" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="10" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="11" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_gravity="fill_horizontal"
                android:text="12" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

Есть ли способ сделать это с помощью GridLayout, я пытался использовать GridLayout, но он работает только с 2 кнопками подряд, а с 3 не работает, я использовал код в этот ответ?


person user4o01    schedule 07.01.2013    source источник
comment
GridLayout будет работать с 3 кнопками подряд, если вы укажете android:columnCount=3   -  person IgorGanapolsky    schedule 27.12.2013
comment
Вы не можете, но попробуйте с макетом таблицы, это очень похоже. Ссылка.   -  person Mr. Potatoe    schedule 04.03.2016


Ответы (2)


Вам не понравится мой ответ, но, прямо скажем, вы не можете. GridLayout не поддерживает какой-либо тип «взвешивания» макета за скажем. Для текста обычно можно использовать параметры app:layout_gravity, но для таких элементов, как кнопки, это не сработает.

Лучше всего просто использовать линейные макеты внутри GridLayout. На самом деле это не сильно улучшает то, что у вас уже есть, но, по крайней мере, вы можете избавиться от первого линейного макета, который у вас есть. Все, что вам нужно, это GridLayout вверху, а затем 1 линейный макет для каждого ряда кнопок. Это повысило бы производительность.

http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html

Раздел «Эмуляция функций из других макетов» намекает на то, что использование LinearLayout в GridLayout для этого сценария является лучшим вариантом.

person Razgriz231    schedule 07.02.2013

Вы должны изменить GridLayout android:rowCount="3". попробуйте эту ссылку и это

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="3" >

    <TextView
        android:text="1,1" />

    <TextView
        android:text="1,2" />

    <TextView
        android:text="1,3" />


    <TextView
        android:text="2,1" />

    <TextView
        android:text="2,2" />

    <TextView
        android:text="2,3" />

    <TextView
        android:text="3,1" />

    <TextView
        android:text="3,2" />

    <TextView
        android:text="3,3" />


</GridLayout>

Изменить: прочитайте эту ссылку. , ссылка

person Zohaib    schedule 07.01.2013
comment
он будет делать каждые 3 текстовых просмотра подряд, но они не будут заполнять всю ширину родителя !!!! - person user4o01; 08.01.2013
comment
@ user4o01 проверьте эту ссылку, может быть, она поможет. /2012/02/android-gridview-layout-tutorial - person Zohaib; 08.01.2013
comment
мой друг, речь пойдет не о GridView, а не о GridLayout - person user4o01; 08.01.2013