Android:防止ImageButton覆盖屏幕?
问题描述:
我有以下LinearLayout
,其中包含相机源预览,并在相机预览顶部绘制覆盖。预览是全屏。Android:防止ImageButton覆盖屏幕?
现在,当我尝试添加一个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开发者文档讨论的布局和摄像头预览: 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>
如果我理解你正确的问题,你有这应该切换到摄像头,是不是目前在使用IE浏览器,如果你正在拍摄图片的前置摄像头,如果你点击按钮的另一个按钮它应该切换到后置摄像头,反之亦然。那是对的吗?
如果是这种情况,也许这是另一种可能性(请记住在下面的第一个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>
如果你希望它被覆盖,检查出[FrameLayout里(http://developer.android.com/reference/ Android设备/部件/ FrameLayout.html)。 –
谢谢,我能够找到http://stackoverflow.com/questions/29072946/how-do-i-create-overlay-button-in-my-android-layout这帮助我了解不同组件之间的关系在我的布局。 –
太棒了,很高兴你明白了。 –