Почему мой ImageView получает высоту 1 пиксель, используя ConstraintLayout? - PullRequest
0 голосов
/ 23 апреля 2019

ОБНОВЛЕНО Я взял образец макета, который был опубликован @theThapa, который, казалось, работал, и внес в него два изменения: я вынул линии подкрашивания android: и заменил один вектор, который можно нарисовать, для трех элементов в макете:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout     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:id="@+id/media_audio_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    >

    <ImageView
        android:id="@+id/topImageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/linked_hands_transluscent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:ignore="ContentDescription"
        />

    <LinearLayout
        android:id="@+id/actionButtonsContainer"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        android:gravity="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/topImageView"
        >

        <ImageButton
            android:id="@+id/btnRecord"
            android:layout_width="50dp"
            android:layout_height="50dp"
            tools:src="@drawable/ic_file_download_black_24dp"
            tools:ignore="ContentDescription"
            />

        <ImageButton
            android:id="@+id/btnPlay"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginStart="8dp"
            tools:ignore="ContentDescription"
            tools:src="@drawable/ic_file_download_black_24dp"
            />

        <ImageButton
            android:id="@+id/btnStop"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginStart="8dp"
            tools:ignore="ContentDescription"
            tools:src="@drawable/ic_file_download_black_24dp"
            />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/deleteSaveCancelContainer"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        android:gravity="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvMessage"
        >

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            tools:text="delete"
            />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btnSave"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginStart="8dp"
            tools:text="save"
            />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginStart="8dp"
            tools:text="cancel"
            />

    </LinearLayout>

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:gravity="start|center_vertical"
        app:layout_constraintTop_toBottomOf="@+id/actionButtonsContainer"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:text="Press the Microphone to record your description. Press the Play and Stop buttons to review your recording"/>

Я не вижу правильных результатов, как то, что было опубликовано в качестве ответа ниже Мне действительно интересно, есть ли разница в версиях Android Studio и / или ConstraintLayout в игре (я использую последнюю версию ConstraintLayout-2.0.0-alpha5)

Портрет: enter image description here

Пейзаж: enter image description here

ОБНОВЛЕНО Я обнаружил, что если я «предварительно загрузил» изображение в ImageView, а затем заменим изображение на то, которое мне нужно, то компоновка будет выполнена соответствующим образом. Я все еще думаю, что это похоже на обходной путь, который мне не нужно было делать, но, по крайней мере, я заставил эту часть работать.

Теперь вторая часть моего поста - где альбомная ориентация не выровнена правильно - все еще проблема. Почему так себя ведет?

Исходное сообщение Я использую ConstraintLayout для разработки экрана, который показывает AppCompatImageView, разделительную линию, некоторые кнопки для записи / воспроизведения аудио и некоторые кнопки для удаления / сохранения / отмены. В Android Studio превью выглядит отлично:

enter image description here

Однако во время выполнения я вообще не вижу видимого изображения. Метод loadImageIntoImageView (), используемый для загрузки изображения в ImageView, работает правильно в других контекстах с этим же изображением, поэтому я считаю, что это как-то связано с ConstraintLayout. Я поставил в журнале, как это:

    AppCompatImageView view = findViewById(R.id.media_image_view);
    InTouchUtils.loadImageIntoImageView(pathToMedia, this, view);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            // Make sure the new entry is visible
            Timber.d("Lifecycle: ImageView size is: w:%d, h:%d", view.getWidth(), view.getHeight());

        }
    });

и кажется, что ImageView получает полную ширину (1440 пикселей) устройства, на котором я тестирую (Samsung S7 Edge), но только высоту 1 пиксель:

enter image description here

Другая вещь, которую я не понимаю, это то, что когда я меняю ориентацию на альбомную в Android Studio, результат предварительного просмотра совсем не тот, который я ожидал (и на самом деле это не то, что происходит на устройстве - я получаю тот же результат) как в портретной ориентации):

enter image description here

Вот мой макет:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:id="@+id/media_audio_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/media_image_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/grandparents_grandchildren_transluscent"
        app:layout_constrainedHeight="true"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintBottom_toTopOf="@+id/annotation_controls_separator"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        tools:ignore="all" />

    <View
        android:id="@+id/annotation_controls_separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:background="?android:attr/dividerVertical"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintBottom_toTopOf="@+id/progress_recording_layout"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/media_image_view" />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/progress_recording_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintBottom_toTopOf="@+id/media_buttons_layout"
        app:layout_constraintEnd_toEndOf="@+id/media_buttons_layout"
        app:layout_constraintStart_toStartOf="@+id/media_buttons_layout"
        app:layout_constraintTop_toBottomOf="@+id/annotation_controls_separator">

        <ProgressBar
            android:id="@+id/audioProgress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/recordingLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="@dimen/text_margin_small"
            android:paddingEnd="@dimen/text_margin_small"
            android:text="@string/media_recording_label"
            android:textAppearance="@android:style/TextAppearance.Material.Small"
            android:textColor="@color/colorAccent"
            android:visibility="gone" />

    </androidx.appcompat.widget.LinearLayoutCompat>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/media_buttons_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintBottom_toTopOf="@+id/media_buttons_explanation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progress_recording_layout">

        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/media_record_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/description_media_record_start"
            android:src="@drawable/ic_mic_black_24dp" />

        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/media_play_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/description_media_record_play"
            android:enabled="false"
            android:src="@drawable/ic_play_arrow_black_24dp" />

        <androidx.appcompat.widget.AppCompatImageButton
            android:id="@+id/media_stop_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/description_media_record_stop"
            android:enabled="false"
            android:src="@drawable/ic_stop_black_24dp" />

    </androidx.appcompat.widget.LinearLayoutCompat>

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/media_buttons_explanation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/text_margin_small"
        android:paddingEnd="@dimen/text_margin_small"
        android:text="@string/media_record_explanation_label"
        android:textAppearance="@android:style/TextAppearance.Material.Small"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_view1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/media_buttons_layout" />

    <View
        android:id="@+id/horizontal_view1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:background="?android:attr/dividerVertical"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintBottom_toTopOf="@+id/dismissButtons"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/media_buttons_explanation" />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/dismissButtons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintTop_toBottomOf="@+id/horizontal_view1"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_view2"
        style="?android:attr/buttonBarStyle"
        android:gravity="top|center"
        android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/recording_delete_button"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:text="@string/media_delete_label" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/recording_save_button"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:text="@string/media_save_label" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/recording_cancel_button"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/media_cancel_label" />

    </androidx.appcompat.widget.LinearLayoutCompat>
    <View
        android:id="@+id/horizontal_view2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintVertical_weight="1"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/dismissButtons" />
</androidx.constraintlayout.widget.ConstraintLayout>

Мне кажется, что я, должно быть, что-то делаю явно неправильно, но я просто не вижу этого.

1 Ответ

0 голосов
/ 25 апреля 2019

@ tfrysinger см. Ниже xml.Я также приложил скриншоты. Форматирование не самое лучшее, пожалуйста, не против.

<?xml version="1.0" encoding="utf-8"?>

 <ScrollView 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:layout_width="match_parent"
        android:layout_height="wrap_content"

    <androidx.constraintlayout.widget.ConstraintLayout     
      android:id="@+id/media_audio_root_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="16dp"
      >

      <ImageView
        android:id="@+id/topImageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/ic_file"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:ignore="ContentDescription"
        />

      <LinearLayout
        android:id="@+id/actionButtonsContainer"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        android:gravity="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/topImageView"
        >

         <ImageButton
          android:id="@+id/btnRecord"
          android:layout_width="50dp"
          android:layout_height="50dp"
          android:tint="@color/gray_dark"
          tools:src="@drawable/ic_file"
          tools:ignore="ContentDescription"
          />

        <ImageButton
          android:id="@+id/btnPlay"
          android:layout_width="50dp"
          android:layout_height="50dp"
          android:layout_marginStart="8dp"
          android:tint="@color/gray_dark"
          tools:ignore="ContentDescription"
          tools:src="@drawable/settings_icon"
        />

     <ImageButton
        android:id="@+id/btnStop"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:tint="@color/gray_dark"
        tools:ignore="ContentDescription"
        tools:src="@drawable/ic_sync"
        />

    </LinearLayout>

    <LinearLayout
      android:id="@+id/deleteSaveCancelContainer"
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:layout_marginTop="16dp"
      android:orientation="horizontal"
      android:gravity="center"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/tvMessage"
      >

      <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:tint="@color/gray_dark"
        tools:text="delete"
        />

      <androidx.appcompat.widget.AppCompatButton
         android:id="@+id/btnSave"
         android:layout_width="wrap_content"
         android:layout_height="50dp"
         android:layout_marginStart="8dp"
         android:tint="@color/gray_dark"
         tools:text="save"
         />

      <androidx.appcompat.widget.AppCompatButton
         android:id="@+id/btnCancel"
         android:layout_width="wrap_content"
         android:layout_height="50dp"
         android:layout_marginStart="8dp"
         android:tint="@color/gray_dark"
         tools:text="cancel"
      />

  </LinearLayout>

  <androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:gravity="start|center_vertical"
    app:layout_constraintTop_toBottomOf="@+id/actionButtonsContainer"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    tools:text="Press the Microphone to record your description. Press the Play and Stop buttons to review your recording"
    />

  </androidx.constraintlayout.widget.ConstraintLayout> 

</ScrollView>

enter image description here

enter image description here

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...