【问题标题】:Unable to add TextView into main xml Layout无法将 TextView 添加到主 xml 布局中
【发布时间】:2019-02-09 18:13:42
【问题描述】:

我一直在开发一个应用程序,并希望在相机视图下方添加一个文本视图,以便我可以在那里显示一些文本。 但是,由于某种原因,当尝试将文本视图拖入布局时,它不会显示在最终屏幕上。

这是我的代码:

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

【问题讨论】:

  • 你能展示你的演示设计吗?

标签: android android-layout android-camera android-view


【解决方案1】:

布局视图 com.google.android.CameraSourcePreview 高度设置为 'match_parent' ,因此它占用了视口上的所有空间。

尝试给出特定的高度,您应该能够看到在 CameraSourcePreview 下方添加的 textview。

希望对你有帮助。

【讨论】:

  • 嗨 Sahil,感谢您的回复。我尝试将高度更改为 android:layout_height="700px" ,但我仍然无法将文本视图添加到布局中。当我将文本视图拖到屏幕上时,它没有显示出来。输出类似于我的问题中显示的图像。
  • 700px 是一个很大的高度数字,试试 100 px 看看我们是否在正确的轨道上。
  • 嘿,sahil,我通过更改为 seymur 在 cmets 中提到的框架布局解决了这个问题。
【解决方案2】:

您很可能无法添加TextView,因为屏幕上没有足够的空间。

您正在使用LinearLayout,它以垂直 或水平 的方式依次显示所有视图。

您的CameraSourcePreview 的高度和宽度设置为match_parent,这意味着它将在屏幕上完全伸展。但是,在LinearLayout 中,这也意味着没有空间放置下一个视图,因为它将被放置在屏幕之外。

您可以将android:layout_weight="1" 添加到您的CameraSourcePreview。这将允许您的TextView 适合LinearLayout,因为它基本上是您的CameraSourcePreview 告诉其他人它将调整自身大小以允许其他组件适合屏幕。

但是,如果您不希望您的 CameraSourcePreview 根据其他视图调整自身大小,那么您应该考虑使用其他一些布局而不是 LinearLayout。也许像ConstraintLayout 或RelativeLayout 这样的方法会更好,因为它们允许在彼此之上重叠视图。

【讨论】:

    【解决方案3】:

    发生这种情况是因为相机视图的高度占用了显示屏上的整个空间,您可以使用 LinearLayout 的 layout_weight 为 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>
    

    【讨论】:

      【解决方案4】:

      我建议不要使用 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>
      

      【讨论】:

      • 嘿 Seyum,谢谢你的建议。这行得通,但我仍然很好奇为什么我在 android studio 中看不到 textView,但我可以在手机上查看它。我的 android studio 主布局看起来像我在问题中显示的图像。
      猜你喜欢
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多