【问题标题】:Divide the screen into SurfaceView and xml layout将屏幕分为SurfaceView和xml布局
【发布时间】:2019-04-06 00:13:38
【问题描述】:

MyActivity 具有覆盖整个屏幕的setContentView(MySurfaceView)

我想把屏幕分成两部分:屏幕的第一个2/3必须被MySurfaceView占据,最后一个1/3必须被MySurfaceView占据my_activity_layout.xml.

我该怎么做?谢谢。

编辑

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

【问题讨论】:

    标签: android android-layout surfaceview


    【解决方案1】:

    您可以使用线性布局并为校正比率指定布局权重来解决此问题。

    <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>
    

    【讨论】:

    • 交换权重。
    • 谢谢,但我不知道如何将您的解决方案应用于我的案例:(我已经编辑了我的问题以使我的情况更清楚。
    【解决方案2】:

    解决方案:

    要在布局中附加 xml 文件,您可以使用&lt;include&gt; 标签。

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

    ConstraintLayout 的帮助下,您可以拥有问题中所示的功能。当然,也有使用旧版 &lt;LinearLayout&gt; 的解决方案,其中包含称为 weights 的东西,但正如警告所说,Weights 不利于性能

    为什么权重不利于性能?

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

    让我们继续使用&lt;ConstraintLayout&gt; 解决问题。

    假设我们有一个名为 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 % 的屏幕,然后您可以在您的活动上使用 &lt;include&gt; 标签限制您的 SurfaceView 和布局。

    也可以看到需要的结果:

    您可以复制粘贴解决方案,看看它是否按预期工作。

    【讨论】:

    • 谢谢,但我不知道如何将您的解决方案应用于我的案例:(我已经编辑了我的问题以使我的情况更清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多