将屏幕分为SurfaceView和xml布局
Divide the screen into SurfaceView and xml layout
MyActivity
setContentView(MySurfaceView)
覆盖了整个屏幕。
我想把屏幕分成两部分:屏幕的第一个2/3必须被MySurfaceView
占据,最后一个 ]1/3 来自 my_activity_layout.xml
.
我该怎么做?谢谢。
编辑
感谢您的回答,但我不知道如何将它们应用到我的案例中。明确地说,这些是我的对象:
您可以使用线性布局并为校正比率指定布局权重来解决此问题。
<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>
解法:
要在布局中附加 xml 文件,您可以使用 <include>
标签。
重用布局特别强大,因为它允许您创建可重用的复杂布局。例如,yes/no 按钮面板,或带有描述文本的自定义进度条。 More
您可以在 ConstraintLayout
的帮助下获得问题中所示的功能。当然,有些解决方案使用遗留 <LinearLayout>
和称为 权重 的东西,但正如警告所说 权重不利于性能 。
为什么权重对性能不利?
Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.
所以让我们继续使用 <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%,然后您可以使用 [=11= 约束您的 SurfaceView
和布局] 标签在你的 activity.
您还可以看到需要的结果:
您可以复制粘贴解决方案,看看它是否按预期工作。
MyActivity
setContentView(MySurfaceView)
覆盖了整个屏幕。
我想把屏幕分成两部分:屏幕的第一个2/3必须被MySurfaceView
占据,最后一个 ]1/3 来自 my_activity_layout.xml
.
我该怎么做?谢谢。
编辑
感谢您的回答,但我不知道如何将它们应用到我的案例中。明确地说,这些是我的对象:
您可以使用线性布局并为校正比率指定布局权重来解决此问题。
<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>
解法:
要在布局中附加 xml 文件,您可以使用 <include>
标签。
重用布局特别强大,因为它允许您创建可重用的复杂布局。例如,yes/no 按钮面板,或带有描述文本的自定义进度条。 More
您可以在 ConstraintLayout
的帮助下获得问题中所示的功能。当然,有些解决方案使用遗留 <LinearLayout>
和称为 权重 的东西,但正如警告所说 权重不利于性能 。
为什么权重对性能不利?
Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.
所以让我们继续使用 <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%,然后您可以使用 [=11= 约束您的 SurfaceView
和布局] 标签在你的 activity.
您还可以看到需要的结果:
您可以复制粘贴解决方案,看看它是否按预期工作。