无法TextView中添加到主XML布局

问题描述 投票:0回答:4

我一直工作在一个应用程序,我期待在添加摄像头视图下方文本视图,这样我可以显示一些文本那里。但是,由于某种原因,试图拖动文本视图到布局时,它不会在最后一个屏幕上显示出来。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">

  <com.google.android.CameraSourcePreview
  android:id="@+id/preview"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<com.google.android.GraphicOverlay
    android:id="@+id/faceOverlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


  </com.google.android.CameraSourcePreview>


</LinearLayout>

Thxml layout output

android android-layout android-camera android-view
4个回答
1
投票

布局视图com.google.android.CameraSourcePreview高度设定为“match_parent”,所以它吃了所有的视口上的空间。

尝试给特定的高度,你应该可以看到下面的TextView添加CameraSourcePreview。

希望能帮助到你。


0
投票

最有可能的,你不能够添加TextView由于到没有在屏幕上有足够的空间。

您正在使用它显示所有它的观点一个接一个地在垂直或水平的方式后LinearLayout

CameraSourcePreview有它的高度和宽度设置为match_parent,这意味着它会在屏幕上完全伸展。然而,在LinearLayout,这也意味着,有没有空间放置下一个视图,因为它会被放在了屏幕。

您可以添加android:layout_weight="1"CameraSourcePreview。这将使你的TextView以适合LinearLayout,因为它基本上是你的CameraSourcePreview告诉别人它会调整自身以适应其它部件,以适应屏幕上。

但是,如果你不希望基于其他浏览你的CameraSourcePreview来调整自己,那么你应该考虑使用一些其他的布局,而不是LinearLayout中。也许一个如ConstraintLayoutRelativeLayout会更好地工作,因为它们允许在彼此顶部重叠的意见。


0
投票

这是发生由于相机视线的高度正在你可以使用layout_weight的LinearLayout中,使你的TextView一些必要的空间,显示屏上的整个空间。

只是要CameraSourcePreview的高度等于0dp并添加属性

android:layout_weight="1"

因此,这也应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:orientation="vertical">

<com.google.android.CameraSourcePreview
    android:id="@+id/preview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <com.google.android.GraphicOverlay
        android:id="@+id/faceOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</com.google.android.CameraSourcePreview>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="some text here" />

</LinearLayout>

0
投票

而不是使用的LinearLayout我建议使用的FrameLayout这是很容易控制你的情况。另外,也可以通过使用下面的代码上CameraSourcePreview显示的TextView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">

<com.google.android.CameraSourcePreview
    android:id="@+id/preview"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_gravity ="center">
    <com.google.android.GraphicOverlay
        android:id="@+id/faceOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</com.google.android.CameraSourcePreview>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="some text here"
    android:layout_gravity ="center|bottom" />

</FrameLayout>
© www.soinside.com 2019 - 2024. All rights reserved.