Неожиданный черный вид при использовании RelativeLayout - PullRequest
0 голосов
/ 22 февраля 2019

Задача

Я завернул SurfaceView и два Button s в RelativeLayout.Но произошла неизвестная проблема.

Это создает черный вид, который показан на скриншоте.И с конца начало SurfaceView или SurfaceView остаются позади черного представления.

Источник : ( surface_look.xml )

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <SurfaceView
        android:id="@+id/mainSurface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:translationZ="15dp"/>

    <Button
        android:id="@+id/surface_up"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="?"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="10dp"/>

    <Button
        android:id="@+id/surface_down"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="?"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"/>

</RelativeLayout>

Результат ( MainActivity ) Layout Runtime Screenshot

Обновление комментария Скотта Screenshot after changing background Когда я изменяю background цвет RelativeLayout, черный вид меняется на цвет.Это означает, что это фон RelativeLayout.Тогда возникает два вопроса.

  • Почему он не заполняет всю высоту?

  • И почему SurfaceView начинается с конца макета, в который он обернут?

1 Ответ

0 голосов
/ 22 февраля 2019

[Согласно нашему чату в комментариях к вашему вопросу]

Вот как будет выглядеть ваш файл макета surface_look.xml при использовании ConstraintLayout в качестве корневого представления.Попробуйте и посмотрите, решит ли он проблему позиционирования, с которой вы столкнулись при использовании RelativeLayout.

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

<android.support.constraint.ConstraintLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

    <!-- let the constraints of the surface view determine it's width and height -->

    <SurfaceView
        android:id="@+id/mainSurface"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:translationZ="15dp"/>

    <Button
        android:id="@+id/surface_up"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="?"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_margin="10dp"/>

    <Button
        android:id="@+id/surface_down"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="?"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_margin="10dp"/>

</android.support.constraint.ConstraintLayout>
...