将屏幕分为SurfaceView和xml布局 [英] Divide the screen into SurfaceView and xml layout

查看:92
本文介绍了将屏幕分为SurfaceView和xml布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想将屏幕分为两部分:屏幕的第一个 2/3 必须由 MySurfaceView 占据,而最后的 1/3通过 my_activity_layout.xml .

我该怎么做?谢谢.

编辑

感谢您的回答,但我没有如何在我的情况下应用它们.要明确的是,这些是我的对象:

解决方案

解决方案:

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

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

您可以复制粘贴该解决方案,然后查看其是否按预期工作.

MyActivity has setContentView(MySurfaceView) that covers all the screen.

I would like to divide the screen into two parts: the first 2/3 of the screen must be occupied by MySurfaceView and the last 1/3 by my_activity_layout.xml.

How can I do that? Thanks.

EDIT

Thanks for your answers, but I don't have how to apply them in my case. To be clear, these are my objects:

解决方案

Solution:

To attach an xml file in your layout, you can make use of the <include> tag.

Reusing layouts is particularly powerful as it allows you create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text. More

You can have a functionality as shown in the question with the help of ConstraintLayout. Of course, there are solutions using the legacy <LinearLayout> with something called as weights but as the warning says Weights are bad for performance.

Why weights are bad for performance?

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.

So let's get on to the solution using <ConstraintLayout>.

Let's say we have a layout file called my_activity_layout.xml and we use the below code to achieve what we want:

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

As you can see the Guideline help us to get 2/3 i.e 66.666 ~ 67 % of the screen, and then you can constraint your SurfaceView and your layout using <include> tag on your activity.

You can also see the required result:

You can just copy-paste the solution and see if it works as expected.

这篇关于将屏幕分为SurfaceView和xml布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆