2016-03-08 97 views
0

我有以下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> 
+1

如果你希望它被覆蓋,檢查出[FrameLayout裏(http://developer.android.com/reference/ Android設備/部件/ FrameLayout.html)。 –

+0

謝謝,我能夠找到http://stackoverflow.com/questions/29072946/how-do-i-create-overlay-button-in-my-android-layout這幫助我瞭解不同組件之間的關係在我的佈局。 –

+0

太棒了,很高興你明白了。 –

回答

0

這聽起來像你可能已經讀這一點,但谷歌的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>