使用RelativeLayout

时间:2019-02-22 07:54:49

标签: java android android-relativelayout

问题


我将SurfaceView和两个Button包装在RelativeLayout中。但是,发生了未知的问题。

它将创建一个黑色视图,该视图显示在屏幕截图中。从最后开始SurfaceViewSurfaceView停留在黑视图的背面。

来源:( 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

更新Scott的评论 Screenshot after changing background 当我更改background的{​​{1}}颜色时,黑色视图将变为该颜色。这意味着它就是RelativeLayout的背景。然后出现两个问题。

  • 为什么它不能填满整个高度?

  • 为什么RelativeLayout从布局的末尾开始 包裹在里面?

1 个答案:

答案 0 :(得分:1)

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

如果将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>