使用RelativeLayout时出现意外的黑视图

问题描述 投票:0回答:1

问题


I've wrapped a SurfaceView and two Buttons in a RelativeLayout. But, unknown problem happened.

它会创建一个黑色视图,如屏幕截图所示。从最后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当我改变backgroundRelativeLayout颜色时,黑色视图变为颜色。这意味着它是RelativeLayout的背景。比那里出现两个问题。

  • 为什么不填满整个高度?
  • 为什么SurfaceView从布局的末尾开始包裹?
java android android-relativelayout
1个回答
1
投票

[根据我们在您的问题评论中的聊天]

如果使用surface_look.xml作为根视图,这就是你的ConstraintLayout布局文件的样子。尝试一下,看看它是否解决了使用RelativeLayout时遇到的定位问题。

注意您必须包含约束布局依赖关系才能在代码中使用它。

<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>
© www.soinside.com 2019 - 2024. All rights reserved.