将屏幕分为SurfaceView和xml布局

6

MyActivity有一个setContentView(MySurfaceView),覆盖了整个屏幕。

我想把屏幕分成两部分:前2/3的屏幕必须由MySurfaceView占据,最后1/3的屏幕要由my_activity_layout.xml占据。

我该如何做呢?谢谢。

进入图片描述

编辑

感谢您的回答,但我不知道如何在我的情况下应用它们。清楚地说,这些是我的对象:

进入图片描述

进入图片描述

2个回答

2

解决方案:

要在布局中附加一个xml文件,您可以使用<include>标记。

重用布局特别强大,因为它允许您创建可重用的复杂布局。例如,带有是/否按钮面板或自定义进度条和说明文本的布局。 更多信息

您可以使用ConstraintLayout来实现问题中所示的功能。当然,也有使用旧版<LinearLayout>和称为权重的内容的解决方案,但正如警告所说,使用权重会影响性能

为什么使用权重会影响性能?

布局权重需要对小部件进行两次测量。当具有非零权重的LinearLayout嵌套在具有非零权重的另一个LinearLayout中时,测量次数呈指数增长。

因此,让我们使用<ConstraintLayout>来解决问题。

假设我们有一个名为my_activity_layout.xml的布局文件,并使用以下代码来实现我们想要的效果:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SurfaceView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.67" />

    <include
        android:id="@+id/news_title"
        layout="@layout/my_activity_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

</android.support.constraint.ConstraintLayout>

如您所见,Guideline可以帮助我们获得屏幕的2/3即66.666 ~ 67%,然后您可以使用<include>标签在活动中约束您的SurfaceView和布局。
您还可以查看需要的结果: Desired Result 您只需复制并粘贴该解决方案,然后查看其是否按预期工作。

谢谢,但我不知道如何将你的解决方案应用到我的情况 :( 我已经编辑了我的问题以使我的情况更加清晰。 - Stefano

0
你可以使用线性布局并为校正比例指定布局权重来解决这个问题。
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"/>

    <include layout="my_activity_layout.xml" 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

交换权重。 - aminography
谢谢,但我不知道如何将你的解决方案应用到我的情况 :( 我已经编辑了我的问题以使我的情况更加清晰。 - Stefano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接