SurfaceView
and two Button
s 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>
更新斯科特的评论当我改变background
的RelativeLayout
颜色时,黑色视图变为颜色。这意味着它是RelativeLayout
的背景。比那里出现两个问题。
SurfaceView
从布局的末尾开始包裹?[根据我们在您的问题评论中的聊天]
如果使用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>