【问题标题】:Android: Preventing an ImageButton from covering the screen?Android:防止 ImageButton 覆盖屏幕?
【发布时间】:2016-03-08 21:21:08
【问题描述】:

我有以下LinearLayout,其中包含一个摄像头源预览,以及一个在摄像头预览之上绘制的叠加层。预览是全屏的。

现在,当我尝试添加ImageButton 以切换正在使用的设备摄像头(前/后)时,我无法阻止按钮全屏显示,完全覆盖摄像头预览。我只希望这个按钮大小正常并位于角落,覆盖相机预览,因此用户可以按下它来更改显示为前置或后置摄像头的相机预览。

我已尝试阅读 Layouts documentation、按钮和一些教程,但我的按钮似乎对我更改的任何属性都没有响应。

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

    <foo.bar.ui.camera.CameraSourcePreview
      android:id="@+id/preview"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

        <ImageButton
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:id="@+id/switch_camera"
            android:src="@drawable/ic_switch_camera_black_48dp"
            android:paddingLeft="16dp"
            android:paddingBottom="16dp"
            android:clickable="true"
            android:contentDescription="@string/switch_camera"
            android:baselineAlignBottom="false" />

    <foo.bar.ui.camera.GraphicOverlay
        android:id="@+id/faceOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

  </foo.bar.ui.camera.CameraSourcePreview>

</LinearLayout>

【问题讨论】:

标签: android android-layout imagebutton android-imagebutton


【解决方案1】:

听起来您可能已经阅读过这篇文章,但 Google 的 Android 开发者文档讨论了布局和相机预览: http://developer.android.com/guide/topics/media/camera.html#preview-layout

“以下布局代码提供了一个非常基本的视图,可用于显示相机预览。在此示例中,FrameLayout 元素旨在成为相机预览类的容器。使用此布局类型以便附加图片信息或控件可以叠加在实时相机预览图像上。”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>

<Button
android:id="@+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>

如果我正确理解您的问题,您有另一个按钮应该切换到当前未使用的摄像头,即如果您当前正在使用前置摄像头拍照,如果您点击该按钮它应该切换到后置摄像头,反之亦然。那是对的吗?

如果是这样,也许这是另一种可能性(只要记住在下面的第一个 LinearLayout 中更改 YOURPACKAGENAME 和活动名称)。两个按钮都在 LinearLayout 中组合在一起,FrameLayout 用于在其上方进行相机预览:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="YOURPACKAGENAME.MainActivity"
tools:showIn="@layout/activity_main">

<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_capture"
        android:text="Capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <Button
        android:id="@+id/switch_camera_front_back"
        android:text="Switch Camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

</LinearLayout>

</LinearLayout> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多