Android Lollipop: не может включать внешний макет внутри GridLayout - PullRequest
0 голосов
/ 21 января 2020

Я пытаюсь включить внешний файл макета, который содержит GridLayout внутри GridLayout. Мой код работает для API 23+, но не работает для API 21 или API 22 (Lollipop). Ниже приведен мой код для activity_main. xml, в который я включаю макет textcomparator. xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:layout_weight="1"
        android:fillViewport="true">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:columnCount="1"
            android:orientation="vertical"
            android:rowCount="1"
            tools:context=".MainActivity">

            <!-- Text comparison UI -->
            <include
                android:id="@+id/text_comparator"
                layout="@layout/textcomparator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_row="0"
                android:layout_column="0"
                android:layout_weight="1"
                android:visibility="visible" />

        </GridLayout>
    </ScrollView>
</LinearLayout>

Ниже приведен мой код для textcomparator. xml:

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

        <EditText
            android:id="@+id/textbox"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:gravity="left|top"
            android:hint="Text 1"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:text="" />

        <EditText
            android:id="@+id/textbox2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_row="0"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:gravity="left|top"
            android:hint="Text 2"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:text="" />
    </GridLayout>
</LinearLayout>

Ниже приведен скриншот для API 22 ( не работает ): enter image description here Ниже приведен скриншот для API 23 ( работает ): enter image description here

Ответы [ 3 ]

1 голос
/ 24 января 2020

Кажется, я вспоминаю, что реализация GridLayout в рамках была немного ошибочной до API 23. TextViews имеют нулевую ширину в API 21 и 22. (Вы можете см. это в инспекторе макетов Android Studio - Инструменты-> Инспектор макетов .) Это связано с тем, что веса макетов не работают. Вам следует использовать версию AndroidX, которая на самом деле работает лучше.

implementation "androidx.gridlayout:gridlayout:1.0.0"

Версия библиотеки поддержки также будет работать.

Вот то, что вы кодируете, будет выглядеть с AndroidX. Я думаю, что XML для библиотеки поддержки будет таким же, за исключением того, что вы ссылаетесь на версию библиотеки поддержки GridLayout , а не на версию AndroidX.

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:layout_weight="1"
        android:fillViewport="true">

        <androidx.gridlayout.widget.GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            app:columnCount="1"
            app:orientation="vertical"
            app:rowCount="1"
            tools:context=".MainActivity">

            <!-- Text comparison UI -->
            <include
                android:id="@+id/text_comparator"
                layout="@layout/textcomparator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_row="0"
                app:layout_column="0"
                android:layout_weight="1"
                android:visibility="visible" />

        </androidx.gridlayout.widget.GridLayout>
    </ScrollView>
</LinearLayout>

И включенный файл :

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.gridlayout.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:rowCount="1"
        app:columnCount="2">

        <EditText
            android:id="@+id/textbox"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_row="0"
            app:layout_column="0"
            app:layout_columnWeight="1"
            android:gravity="left|top"
            android:hint="Text 1"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:text="" />

        <EditText
            android:id="@+id/textbox2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_row="0"
            app:layout_column="1"
            app:layout_columnWeight="1"
            android:gravity="left|top"
            android:hint="Text 2"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:text="" />
    </androidx.gridlayout.widget.GridLayout>
</LinearLayout>

Быстрый тест на эмуляторе с API 22 показывает следующее:

enter image description here

Это то, что ожидается.

1 голос
/ 24 января 2020

Если вы используете androidx, тогда этот ответ работает, но если вы не используете androidx, следуйте приведенным ниже инструкциям: 1. Добавьте следующую зависимость в ваш app.gradle файл

implementation 'com.android.support:gridlayout-v7:28.0.0' 

2.Замените GridLayout на android.support.v7.widget.GridLayout в textcomparator.xml и activity_main.xml файлы макетов 3.Замените все следующие атрибуты

android:rowCount
android:columnCount
android:orientation
android:layout_row
android:layout_column
android:layout_columnWeight

этими атрибутами

app:rowCount
app:columnCount
app:orientation
app:layout_row
app:layout_column
app:layout_columnWeight

Ваш окончательный код должен выглядеть следующим образом: activity_main. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:layout_weight="1"
        android:fillViewport="true">

        <android.support.v7.widget.GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            app:columnCount="1"
            app:orientation="vertical"
            app:rowCount="1"
            tools:context=".MainActivity">

            <!-- Text comparison UI -->
            <include
                android:id="@+id/text_comparator"
                layout="@layout/textcomparator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_row="0"
                app:layout_column="0"
                android:layout_weight="1"
                android:visibility="visible" />

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

textcomparator. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:rowCount="1"
        app:columnCount="2">

        <EditText
            android:id="@+id/textbox"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_row="0"
            app:layout_column="0"
            app:layout_columnWeight="1"
            android:gravity="left|top"
            android:hint="Text 1"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:text="" />

        <EditText
            android:id="@+id/textbox2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_row="0"
            app:layout_column="1"
            app:layout_columnWeight="1"
            android:gravity="left|top"
            android:hint="Text 2"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:text="" />
    </android.support.v7.widget.GridLayout>
</LinearLayout>

Я проверил его на API 21, и он работает хорошо.

0 голосов
/ 21 января 2020

Попробуйте использовать Gradle, и в вашем файле build.gradle добавьте следующий раздел в конце:

dependencies {
  implementation 'com.android.support:gridlayout-v7:28.0.0'
  implementation 'com.android.support:appcompat-v7:28.0.0'
}

Затем выполните задачу сборкиDebug gradle.

...