【问题标题】:Is it possible to have Camera Preview and TextView on the same screen?是否可以在同一屏幕上拥有相机预览和 TextView?
【发布时间】:2011-11-26 04:42:52
【问题描述】:

我想知道是否有某种方法可以让相机预览填充屏幕的一部分,并且在底部有一个文本视图框,我可以稍后在其中添加文本。 我还没有任何代码,因为在继续编写代码之前,我的团队仍在评估这是否可行。

编辑 2:现在我在玩了 android:gravity 之后终于可以在屏幕上看到文字了。但是TextView总是显示在屏幕的顶部,有什么办法可以把它移到底部吗?

编辑 3:将 android:layout_height 更改为 fill_parent 而不是 wrap_content 修复了定位问题

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
      <SurfaceView android:id="@+id/preview_view"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_centerInParent="true"/>
      <com.google.zxing.client.android.ViewfinderView
           android:id="@+id/viewfinder_view"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:background="@color/transparent"/>
      ........
      ........
      <TextView android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:gravity="bottom" 
           android:text="Header text"/>

</FrameLayout>

【问题讨论】:

    标签: android camera preview textview


    【解决方案1】:

    是的,这是可能的。您应该为此使用FrameLayout。这是一个例子:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent">
    
      <SurfaceView android:id="@+id/preview_view"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"
                   android:layout_centerInParent="true"/>
    
      <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center_horizontal" 
                android:text="Header text"/>
    
    </FrameLayout>
    

    【讨论】:

    • 感谢您的回答,我应该提到我得到的相机预览实际上是从 Zxing 的条形码扫描仪扩展而来的。我想减小相机预览大小,并在底部有一个 Textview。我已经尝试按照你们俩的建议添加 TextView,但我看不出有任何区别。供您参考,我将附上zxing xml文件
    • 现在可以使用了!但有个小问题,我编辑了我的帖子以包含我正在处理的代码。重力似乎无法正常工作:/ 我是否需要某个地方的线性布局?
    • 您使用了android:layout_height="wrap_content",而水平重力应该是fill_parent 才有意义/有效。
    【解决方案2】:

    是的,可以这样:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
    
      <android.view.SurfaceView
      android:id="@+id/surface"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" />
    
    <TextView
        android:id = "@+id/txtview"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text = "TextView"
        android:padding = "7dp"
        android:gravity = "bottom" />
    
    </FrameLayout>
    

    【讨论】:

    • 有没有办法将两个答案都设置为已接受.. 我只能设置一个。感谢您的帮助,我现在可以使用它了,但是我无法将 textview 的位置更改为底部.. 它始终显示在顶部。有什么建议吗?
    【解决方案3】:

    感谢此线程上的帖子,这是一个使用简单 LinearLayout 的变体,它限制了相机显示的尺寸

    这减小了“扫描方块”的大小

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:background="@color/sea_blue_green_dark"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:padding="10dp"
            android:textColor="@color/white"
            android:textSize="28sp"
            android:text="Scan Code Now"/>
    
        <me.dm7.barcodescanner.zxing.ZXingScannerView
            android:id="@+id/scanner"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/white"/>
    
        <!--
        Add this view to prevent camera from taking up remaining vertical space
    
        Although seems to be better UX when user can still see camera outside of "Scanner Box Target". 
        Leaving here for reference
        -->
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#8BC34A"/>
    
    </LinearLayout>
    

    =======

    这将生成以下布局,其中白色块成为相机区域:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多